Apparently I am too stupid to enter the correct search terms, b/c I think that my question is not unique at all.
How to refer to a variable by string in the i
part of data.table
? with
and ..x
are all good for the j
part, but what would be the equivalent in the i
part? Do I have to use evil eval
(pun intended ;)
library(data.table)
dt <- data.table(x = 1:4, y = 4:1)
my_filter_fun <- function(var = names(dt)) {
var <- match.arg(var)
dt[eval(parse(text = paste(var, "== 1")))]
}
my_filter_fun("x")
my_filter_fun("y")
What is the idiomatic way in data.table
to do so? Coming from dplyr
I think I am looking for the equivalent of quosures for data.table
?
Bonus question: how could i implement my_filter_fun
such that such a call
my_filter_fun(x > 1)
would return the same result as
dt[x > 1]