I've got multiple massive data sets in data.tables and some other data.tables containing lists of what amount to essentially expressions I'd like to execute against them using something like :=
.
Sample data:
library(data.table)
tt = data.table(date=c(2011, 2012, 2013, 2014), count=c(2774343,4655434,648113695, 357733805))
date count
1: 2011 2774343
2: 2012 4655434
3: 2013 648113695
4: 2014 357733805
Sample transformation table. Some columns may be new, others may be modifying pre-existing columns. I need them to take full advantage of the 'with' feature, meaning they need to reference existing columns even if they are creating new ones.
xform=data.table(var=c("date2", "count2"), val=c("date - 2000", "count / 1000"))
var val
1: date2 date - 2000
2: count2 count / 1000
I just can't imagine the magic formula needed to get this to work. I've tried various combinations of lapply, parse, eval, etc. inside [.data.table
using :=
.
My last hope was this:
> xform[,expr := lapply(val, FUN=function(x) parse(text=x))]
> tt[,(xform$var) := eval(xform$expr)]
Error in eval(expr, envir, enclos) : attempt to apply non-function
The trick is my input data is massive and contains up to 100 columns, and while some of the transformations may be trivial, others may be sophisticated.
In this case, the output should be something like:
date count date2 count2
1: 2011 2774343 11 2774.343
2: 2012 4655434 12 4655.434
3: 2013 648113695 13 648113.695
4: 2014 357733805 14 357733.805
Thanks in advance for any help!