Accessing columns in data.table using a character vector of column names

Solution 1:

You can use the data.table syntax .. which "looks up one level" (as in the Unix terminal) for the variable:

> all.equal(DT[,list(x,y)], DT[, ..cols])
[1] TRUE
> all.equal(DT[,.SD[,list(x,y)][min(v)]], DT[,.SD[ ,min(v)], .SDcols = cols])
[1] TRUE

More details under FAQ 1.6 I believe: http://datatable.r-forge.r-project.org/datatable-faq.pdf