Filter data.table using inequalities and variable column names

5.3k views Asked by At

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.

3

There are 3 answers

2
Carl Witthoft On BEST ANSWER

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.

4
Simon O'Hanlon On

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 ) ]
0
MattLBeck On

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

dt[dt[[mycol]] > 2]