I've the following data.table
structure(list(val1 = c(1, 2, 1, 3, 4, 5, 3), val2 = c(4, 5, 6, 4, 2, 4, 5)), .Names = c("val1", "val2"), row.names = c(NA, -7L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0xedae28>)
What I would like to do is filter the rows in it based on a criteria defined by other variables. For example, I might want all rows that have val1 >= 1
. This is easily done as
x[val1 > 1,]
However, I want to be able to specify the column name (i.e. either val1
or val2
) as a variable and the filter value as a variable. I tried
cname = 'val1'
cutoff = 1
x[(cname >= cutoff),] # Try 1
x[(cname) >= (cutoff),] # Try 2
x[eval(cname > cutoff),] # Try 3
but it just gave back the original data.table
.