Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data.table within group id [duplicate]

Tags:

r

data.table

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
like image 976
Tyler Rinker Avatar asked Jan 07 '23 15:01

Tyler Rinker


1 Answers

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
# ...
like image 194
jlhoward Avatar answered Jan 16 '23 01:01

jlhoward