JBJ
JBJ

Reputation: 876

Supply arguments to data.table as (1) vector of strings AND (2) variablenames

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:

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]

Upvotes: 4

Views: 1735

Answers (1)

IRTFM
IRTFM

Reputation: 263331

I wasn't able to figure out what you were trying to do with the matrix-multiplication, but this shows how to create new variables with varying and fixed inputs to a function:

D <- data.table(id=1:3, fix=1:3, dyn1=1:3, dyn2=1:3) 
setkey(id)

foo <-function(fix, dynvector){ fix* dynvector}
D[, paste("new",1:2,sep="_") := lapply( c(dyn1,dyn2), foo, fix=fix), by=id]
#----------
> D
   id fix dyn1 dyn2 new_1 new_2
1:  1   1    1    1     1     1
2:  2   2    2    2     4     4
3:  3   3    3    3     9     9

So you need to use a vector of character values to get columns. This is a bit of an extension to this question: Why do I need to wrap `get` in a dummy function within a J `lapply` call?

> D <- data.table(id=1:3, fix=1:3, dyn1=1:3, dyn2=1:3) 
> setkey(D, id)
> id1 <- parse(text=cn)
> foo <-function( fix, dynvector){  fix*dynvector}
> D[, paste("new",1:2,sep="_") := lapply( sapply( cn, function(x) {get(x)}) , foo, fix=fix) ]
Warning message:
In `[.data.table`(D, , `:=`(paste("new", 1:2, sep = "_"), lapply(sapply(cn,  :
  Supplied 2 columns to be assigned a list (length 6) of values (4 unused)
> D
   id fix dyn1 dyn2 new_1 new_2
1:  1   1    1    1     1     2
2:  2   2    2    2     2     4
3:  3   3    3    3     3     6

You could probably use the methods in create an expression from a function for data.table to eval as well.

Upvotes: 1

Related Questions