Ina.Quest
Ina.Quest

Reputation: 165

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.

Upvotes: 1

Views: 4441

Answers (1)

nicola
nicola

Reputation: 24480

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)]

Upvotes: 6

Related Questions