组ID中的data.table

时间:2015-09-07 17:51:55

标签: r data.table

我有一个带有n个分组变量的data.table(在本例中为2)。我想为每个组添加一个标识符列,如下面所需的输出所示。我试过:=:N`我知道为什么不起作用但不知道如何实现它:

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

所需输出

##     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

1 个答案:

答案 0 :(得分:7)

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
# ...