Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter data.table using inequalities and variable column names

I have a data.table that i want to filter based on some inequality criteria:

dt <- data.table(A=letters[1:3], B=2:4)
dt
#    A B
# 1: a 2
# 2: b 3
# 3: c 4

dt[B>2]
#    A B
# 1: b 3
# 2: c 4

The above works well as a vector scan solution. But I can't work out how to combine this with variable names for the columns:

mycol <- "B"
dt[mycol > 2]
#    A B      // Nothing has changed
# 1: a 2
# 2: b 3
# 3: c 4

How do I work around this? I know I can use binary search by setting keys using setkeyv(dt, mycol) but I can't see a way of doing a binary search based on some inequality criteria.

like image 460
MattLBeck Avatar asked Dec 13 '13 14:12

MattLBeck


3 Answers

OK, then, Use get(mycol) because you want the argument to dt[ to be the contents of the object "mycol" . I believe dt[mycol ...] looks for a "mycol" thingie in the data.table object itself, of which of course there is no such animal.

like image 192
Carl Witthoft Avatar answered Sep 22 '22 17:09

Carl Witthoft


There is an accesor function provided for this. j is evaluated in the frame of X, i.e. your data.table, unless you specify with = FALSE. This would be the canonical way of doing this.

dt[ , mycol , with = FALSE ]
   B
1: 2
2: 3
3: 4

Return column, logical comparison, subset rows...

dt[ c( dt[ , mycol , with = FALSE ] > 2 ) ]
like image 31
Simon O'Hanlon Avatar answered Sep 24 '22 17:09

Simon O'Hanlon


Another alternative is to use ]] to retrieve B as a vector, and subset using this:

dt[dt[[mycol]] > 2]
like image 25
MattLBeck Avatar answered Sep 26 '22 17:09

MattLBeck