当前位置: 动力学知识库 > 问答 > 编程问答 >

r - assign unique ID name to unique rows with multiple columns

问题描述:

I apologize, not sure how to insert a data.table into the question box.

I have a data set with a ton of rows like this:

phylum class family order genus species

A B C D E NA

A B C D E NA

A B C D NA NA

A B C D E F

A B C D NA NA

A B C D E F

I would like each matching row to be assigned a unique ID for example:

 ID phylum class family order genus species

1 A B C D E NA

1 A B C D E NA

2 A B C D NA NA

3 A B C D E F

2 A B C D NA NA

3 A B C D E F

I have tried using GRP in a variety of ways but its not working.

For example:

DT2 = DT[,i:=.GRP,by=key(DT)]

I have looked at other samples but everything is assigning IDs based on a single or only 2 columns value and I want to use 6 different ones. Any help is greatly appreciated.

网友答案:

A solution with base R:

df2 <- unique(df)
df2$ID <- 1:nrow(df2)
merge(df, df2)

or using data.table:

dt[, ID := .GRP, by = names(dt)]
分享给朋友:
您可能感兴趣的文章:
随机阅读: