当我在一个表达式中按data.table子集并将其按变量分组时,出现以下错误:
> dt1 = data.table(a = 1:2, b1 = 1, key = "a")
> dt2 = data.table(a = 1:2, b2 = 1, key = "a")
> dt1[dt2, .N, by = c("b1", "b2")]
Error in eval(bysub, xss, parent.frame()) : object 'b2' not found
当我仅按dt1中的变量分组时,它起作用:
> dt1[dt2, .N, by = "b1"]
b1 N
1: 1 2
如果我将其分为两个连续的表达式,它将起作用:
> dt1[dt2][, .N, by = c("b1", "b2")]
b1 b2 N
1: 1 1 2
我可以将它变成单个表达式吗?