I need to select specific columns of a data.table
with a vector of column names or positions.
library(data.table)
DT <- data.table(cbind(A=rnorm(50),B=rnorm(50),C=rnorm(50),D=rnorm(50)))
Indexing column's "A" and "C" works well this way.
DT[,c("A","C")]
but if i specify a variable and try to index it fails.
mycols <- c("A","C")
DT[,mycols]
I am forced to use with=FALSE
but i dont want to, because with=FALSE
treats DT like a data.frame
and i loose all the performance advantages (speed) of data.table
.
My questions are. Why does data.table
accept a vector of characters the former way but not the latter? Is there a solution that preserves the performance advantages of data.table
?
Thanks