Imagine you want to apply a function row-wise on a data.table. The function's arguments correspond to fixed data.table columns as well as dynamically generated column names.
Is there a way to supply fixed and dynamic column names as argument to a function while using data.tables?
The problems are:
- Both, variablenames and dynamically generated strings as argument to a function over a datatable
- The dynamic column name strings are stored in a vector with > 1 entries (get()won't work)
- The dynamic column's values need to be supplied as a vector to the function
This illustrates it:
library('data.table')
# Sample dataframe
D <- data.table(id=1:3, fix=1:3, dyn1=1:3, dyn2=1:3) #fixed and dynamic column names
setkey(D, id)
# Sample function
foo <-function(fix, dynvector){ rep(fix,length(dynvector)) %*% dynvector}
# It does not matter what this function does.
# The result when passing column names not dynamically
D[, "new" := foo(fix,c(dyn1,dyn2)), by=id]
#    id fix dyn1 dyn2 new
# 1:  1   1    1    1   2
# 2:  2   2    2    2   8
# 3:  3   3    3    3  18
I want to get rid of the c(dyn1,dyn2). I need to get the column names dyn1, dyn2 from another vector which holds them as string.
This is how far I got:
# Now we try it dynamically
cn <-paste("dyn",1:2,sep="")   #vector holding column names "dyn1", "dyn2"
# Approaches that don't work
D[, "new" := foo(fix,c(cn)), by=id]            #wrong as using a mere string
D[, "new" := foo(fix,c(cn)), by=id, with=F]    #does not work
D[, "new" := foo(fix,c(get(cn))), by=id]       #uses only the first element "dyn1"
D[, "new" := foo(fix,c(mget(cn, .GlobalEnv, inherits=T))), by=id]       #does not work
D[, "new" := foo(fix,c(.SD)), by=id, .SDcols=cn]       #does not work
I suppose mget() is the solution, but I know too less about scoping to figure it out.
Thanks! JBJ
Update: Solution
based on the answer by BondedDust
    D[, "new" := foo(fix,sapply(cn, function(x) {get(x)})), by=id]
 
     
    