data.table 使用 setkey 的默认外连接,计算行数(列出键的频率)
data.table default outer join with setkey, counting rows (tabulate frequency of keys)
我有两个data.tables。我想用一个中的键来计算另一个中的出现次数。
DT1 <- data.table(A = c("v1","v2","v1","v2","v3","v4","v5","v6","v1","v2"),
B = c(1,11,111,2,22,222,3,33,333,4))
DT2 <- data.table(C = c("v1","v3","v99"))
setkey(DT1,A)
setkey(DT2,C)
使用 DT2 在 DT1 中查找行 returns 外连接。
nrow(DT1[DT2,]) #returns 5 (v1*3, v3*1, v99) "v99" not in DT2
DT1[DT2,]
A B
1: v1 1
2: v1 111
3: v1 333
4: v3 22
5: v99 NA
是否可以使用相同的语法强制左连接?
I want to use the keys in one to count the occurrences in another.
DT1[DT2, .N, on=c(A="C"), by=.EACHI]
A N
1: v1 3
2: v3 1
3: v99 0
我有两个data.tables。我想用一个中的键来计算另一个中的出现次数。
DT1 <- data.table(A = c("v1","v2","v1","v2","v3","v4","v5","v6","v1","v2"),
B = c(1,11,111,2,22,222,3,33,333,4))
DT2 <- data.table(C = c("v1","v3","v99"))
setkey(DT1,A)
setkey(DT2,C)
使用 DT2 在 DT1 中查找行 returns 外连接。
nrow(DT1[DT2,]) #returns 5 (v1*3, v3*1, v99) "v99" not in DT2
DT1[DT2,]
A B
1: v1 1
2: v1 111
3: v1 333
4: v3 22
5: v99 NA
是否可以使用相同的语法强制左连接?
I want to use the keys in one to count the occurrences in another.
DT1[DT2, .N, on=c(A="C"), by=.EACHI]
A N
1: v1 3
2: v3 1
3: v99 0