Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data.table and character vectors

Tags:

r

data.table

In my work, I often refer to lists of variables as just one character vector.

A <- data.table(var1 = 1:10, var2 = 11:20, var3 = 21:30)
vecvar <- c("var1", "var2", "var3")

Whenever possible, I want to use vecvar rather than enumerate the variables, as this makes my code more robust to future applications to slightly different lists of variables.

I recently discovered data.table, and as much as I love the general elegance of the syntax and the efficiency gains, I find that it clashes a bit with my inclination outlined above. Indeed, A[, vecvar] won't work.

I am undecided as to the best way to work around this.

A[, vecvar, with=F] will work, but is not always convenient (e.g. A[, list(vecvar, var1+var2), with=F] won't work).

A[, sapply(vecvar, get)] won't work, though A[, sapply(vecvar, function(x) get(x))] will.

I am not stuck, as I have ways to deal with it -- I just want to know what the best way to work with this is, before I pick up bad habits!

like image 258
crayola Avatar asked Mar 25 '11 16:03

crayola


2 Answers

I kind of see what you mean. Does FAQ1.6 help? Also, there were a few threads on datatable-help about using quote()-ed expressions in data.table.

like image 107
Matt Dowle Avatar answered Oct 27 '22 13:10

Matt Dowle


is this what you want to do?

> subset(A, select=vecvar[1:2])
      var1 var2
 [1,]    1   11
 [2,]    2   12
 [3,]    3   13
 [4,]    4   14
 [5,]    5   15
 [6,]    6   16
 [7,]    7   17
 [8,]    8   18
 [9,]    9   19
[10,]   10   20
like image 34
kohske Avatar answered Oct 27 '22 13:10

kohske