如何将多个[] []与多个键链接在一起

时间:2014-05-03 22:15:41

标签: r data.table

我希望根据一列中的匹配对数据进行子集,而使用data.tableJ()!J()函数与其他列不匹配

library(data.table)
DT <- data.table(x = rep(c("a", "b", "c"), each=2000), y=c(rep(c(1,3,6), each = 1)) , key = c("x", "y"))

我希望J()!J()函数提供与以下代码相同的结果:

DT[J("b")][y !=1] 

我尝试了以下操作,但它出现了以下错误:

DT[J("b")][!J(x, 1)]

Error in vecseq(f__, len__, if (allow.cartesian) NULL else as.integer(max(nrow(x),  : 
  Join results in 1920000 rows; more than 4800 = max(nrow(x),nrow(i)). Check for duplicate key values in i, each of which join to the same group in x over and over again. If that's ok, try including `j` and dropping `by` (by-without-by) so that j runs for each group to avoid the large allocation. If you are sure you wish to proceed, rerun with allow.cartesian=TRUE. Otherwise, please search for this error message in the FAQ, Wiki, Stack Overflow and datatable-help for advice.

我尝试了下面的代码,但它没有消除第二个不包含1的条件

DT[J("b")][!J("1")]

1 个答案:

答案 0 :(得分:3)

这个答案来自Arun。所有的功劳归功于Arun

library(data.table)
DT <- data.table(x = rep(c("a", "b", "c"), each=2000), y=c(rep(c(1,3,6), each = 1)) , key = c("x", "y"))

DT["b"][!J(unique(x), 1)]

此子集基于匹配列b中包含x的所有行,并且在列1的所有行中与y不匹配。