Possible Duplicate:
Variably selecting/assigning to fields in a data.table
In following example, I am creating a data table having column name ‘x’ and ‘v’
library('data.table')
DT <- data.table(x = c("b","b","b","a","a"), v = rnorm(5))
I can access values of column ‘x’ by :
DT[ , x]
# [1] "b" "b" "b" "a" "a"
But if I want to access by passing through a variable, it doesn’t work
temp <- "x"
DT[ , temp]
# [1] "x"
There would be multiple columns and I will have to select values for only couple of them. These column names I will be provide by passing through a R module.
Never mind, I got it, it should be:
DT[ , get(temp)]