如何使用R的data.table包来对键值的否定进行子集化?

Eri*_*son 5 r data.table

R的data.table包提供基于键的快速子集值.

所以,例如:

set.seed(1342)

df1 <- data.table(group = gl(10, 10, labels = letters[1:10]),
                  value = sample(1:100))
setkey(df1, group)

df1["a"]
Run Code Online (Sandbox Code Playgroud)

将返回df1中的所有行,其中group =="a".

如果我想在所有行df1其中group != "a".是否有使用的简明语法data.table

Cha*_*ase 8

我想你回答了自己的问题:

> nrow(df1[group != "a"])
[1] 90
> table(df1[group != "a", group])

 a  b  c  d  e  f  g  h  i  j 
 0 10 10 10 10 10 10 10 10 10 
Run Code Online (Sandbox Code Playgroud)

对我来说似乎很简洁?

从MATTHEW编辑:根据评论这是一个矢量扫描.这里这里有一个不加入的习语 ,并且功能请求#1384使它更容易.

编辑:功能请求#1384在data.table 1.8.3中实现

df1[!'a']

# and to avoid the character-to-factor coercion warning in this example (where
# the key column happens to be a factor) :
df1[!J(factor('a'))]
Run Code Online (Sandbox Code Playgroud)