7

I have a data.table with n grouping variables (in this case 2). I want to add an identifier column for each group as seen in the desired output below. I tried :=:.N` and I get why that doesn't work but don't know how to make it happen:

library(data.table)
dat <- data.table::data.table(
    w = 1:16,
    x = LETTERS[1:2],
    y = 1:4
)[, w := NULL][order(x, y)]


##     x y
##  1: A 1
##  2: A 1
##  3: A 1
##  4: A 1
##  5: A 3
##  6: A 3
##  7: A 3
##  8: A 3
##  9: B 2
## 10: B 2
## 11: B 2
## 12: B 2
## 13: B 4
## 14: B 4
## 15: B 4
## 16: B 4


dat[, z := 1:.N, by = list(x, y)]
dat

Desired Output

##     x y z
##  1: A 1 1
##  2: A 1 1
##  3: A 1 1
##  4: A 1 1
##  5: A 3 2
##  6: A 3 2
##  7: A 3 2
##  8: A 3 2
##  9: B 2 3
## 10: B 2 3
## 11: B 2 3
## 12: B 2 3
## 13: B 4 4
## 14: B 4 4
## 15: B 4 4
## 16: B 4 4
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519

1 Answers1

13
dat[, z:=.GRP,by=list(x,y)]
dat
#     x y z
#  1: A 1 1
#  2: A 1 1
#  3: A 1 1
#  4: A 1 1
#  5: A 3 2
#  6: A 3 2
#  7: A 3 2
#  8: A 3 2
#  9: B 2 3
# 10: B 2 3
# ...
jlhoward
  • 58,004
  • 7
  • 97
  • 140