Max Max - 3 months ago 13
R Question

Subset in i by variable name in data.table

Suppose I have a data.table with columns names that are specified in a variable. For example I might have used

dcast
as:

groups <- sample(LETTERS, 2) # i.e. I don't now the values
dt1 <- data.table(ID = rep(1:2, each = 2), group = groups, value = 3:6)
(dt2 <- dcast(dt1, ID~group, value.var = "value"))
# ID D Q
# 1: 1 3 4
# 2: 2 5 6


Now I want to subset based on values in the last two columns, e.g. do something like:

dt2[groups[1] == 3 & groups[2] == 4]
# Empty data.table (0 rows) of 3 cols: ID,D,Q


Is there an easy way?
I found I can do this with keys:

setkeyv(dt2, groups)
dt2[.(3, 4)]
# ID D Q
# 1: 1 3 4


But how do I do something more elaborate, as

dt2[groups[1] > 3 & groups[2] < 7]


?

Answer

You can use get to (from ?get)

search by name for an object

:

dt2[get(groups[1]) > 2 & get(groups[2]) == 4]
#   ID A J
#1:  1 3 4