I am using a vector of column names to select a subset of columns of a data.table. I had the idea if it's possible to basically define conditions in i
which are then applied to all the selected columns.
For example using the mtcars
dataset.
I would like to select the columns cylinder and gear and then would like to filter on all cars which have four cylinders and four gears. Of course I would also need to define if it is and
or or
for the filter, but I am just interested if the idea can be applied somehow in the data.table
context.
# working code
sel.col <- c("cyl", "gear")
dt <- data.table(mtcars[1:4,])
dt[, ..sel.col]
dt[cyl == 4 & gear == 4, ..sel.col]
# Non-working code
dt[ sel.col == 4 , ..sel.col]