r - 如何使用多个键将多个 [ ] [ ] 链接在一起

标签 r data.table

我想根据一列中的匹配对数据进行子集化,而使用 data.table 不匹配另一列中的数据。 , J()!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")]

最佳答案

这个答案来自阿伦。所有的功劳都归功于阿伦

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 的所有行的匹配来对数据进行子集化。在栏目x1 不匹配在列 y 的所有行中.

关于r - 如何使用多个键将多个 [ ] [ ] 链接在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23450376/

相关文章:

r - 根据向量定义的多列中的值选择行

r - 如何将行添加到 Stargazer 表以指示使用固定效果

r - 使用runuser时如何导出变量

每行和每列都有条件的随机值(0 和 1)

r - 如何在不知道索引 R 的情况下在多列中应用 as.Date 函数

r - 带 fst 的 bit64 整数

使用行过滤而不是读取+子集读取 `data.table`

r - 如果索引列名是连接列名的前缀,Data.table join with index 会产生意外的结果

r - 加速 data.table 相交

r - data.table:在组内符号出现之前/之后标记