Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I subset the negation of a key value using R's data.table package?

Tags:

r

data.table

R's data.table package offers fast subsetting of values based on keys.

So, for example:

set.seed(1342)

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

df1["a"]

will return all rows in df1 where group == "a".

What if I want all rows in df1 where group != "a". Is there a concise syntax for that using data.table?

like image 766
Erik Iverson Avatar asked Apr 03 '12 15:04

Erik Iverson


2 Answers

I think you answered your own question:

> 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 

Seems pretty concise to me?

EDIT FROM MATTHEW : As per comments this a vector scan. There is a not join idiom here and here, and feature request #1384 to make it easier.

EDIT: feature request #1384 is implemented in 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'))]
like image 167
Chase Avatar answered Sep 25 '22 08:09

Chase


I would just get all keys that are not "a":

df1[!(group %in% "a")]

Does this achieve what you want?

like image 43
Christoph_J Avatar answered Sep 23 '22 08:09

Christoph_J