data.table
不保留做子集的关键
setkey(DT,a,b,c)
key(DT[,list(a,b)]) # return NULL
有人有解决方法吗(XY 因此 Z 是 data.table)?我真的想这样做=>
Z = X[,list(a, b, c)][Y, mult='last']
注意:我可以
X2 = X[,list(a, b, c)]
setattr(X2,"sorted",c("a","b","c"))
Z = X2[Y, mult='last']
但我不想将 X 复制到 X2
编辑示例:
Y = data.table(a=seq(2,4),key="a")
X = data.table(a=seq(1,5),b=seq(2,6),c=sample(letters,5),key="a,b,c")
X[,list(a, b, c)][Y, mult='last']
Error in `[.data.table`(X[, list(a, b, c)], Y, mult = "last") :
When i is a data.table (or character vector), x must be keyed
更新(eddi):从1.8.11 版开始,此问题已得到修复,密钥保留在第一个子集中,因此结果为:
X[,list(a, b, c)][Y, mult='last']
# a b c
#1: 2 3 k
#2: 3 4 z
#3: 4 5 u