8

Suppose I have an R data.table:

 DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)

and I have a character vector of column names that I would like to extract, or more generally operate on:

cols = c("x","y")

For example, how can I use cols to generate the equivalent of

 DT[,lapply(.SD[,list(x,y)], min) ]

Is there a way to specify the list(x,y) using the cols vector?

DavidR
  • 810
  • 2
  • 8
  • 16

1 Answers1

7

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

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
Chase
  • 67,710
  • 18
  • 144
  • 161
  • Thanks! I changed my question a bit while you were answering it, but the solution still seems good. – DavidR Feb 21 '13 at 17:11
  • Using with=FALSE inside an .SD seems like a useful idiom. If that's a recommended approach for data.table, I wonder if it should be in the data.table or examples or FAQ? @MatthewDowle – DavidR Feb 21 '13 at 17:43
  • 1
    @DavidR Maybe '.SDcols' ? See ?data.table and search this tag for "SDcols". – Matt Dowle Feb 21 '13 at 18:12
  • @DavidR No problem. A particularly good answer on .SDcols from a few days ago not to miss : http://stackoverflow.com/a/14937323/403310 – Matt Dowle Feb 21 '13 at 18:42