title: Supply arguments to data.table as (1) vector of strings AND (2) variablenames
body: <p>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.</p>
  
  <p>Is there a way to supply fixed and dynamic column names as argument to a function while using data.tables?</p>
  
  <p>The problems are:</p>
  
  <ul>
  <li>Both, variablenames and dynamically generated strings as argument to a function over a datatable</li>
  <li>The dynamic column name strings are stored in a vector with > 1 entries (<code>get()</code> won't work)</li>
  <li>The  dynamic column's values need to be supplied as a vector to the function</li>
  </ul>
  
  <p>This illustrates it:</p>
  
  <pre><code>library('data.table')
  # Sample dataframe
  D &lt;- 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 &lt;-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
  </code></pre>
  
  <p>I want to get rid of the <code>c(dyn1,dyn2)</code>. I need to get the column names dyn1, dyn2 from another vector which holds them as string.</p>
  
  <p>This is how far I got:</p>
  
  <pre><code># Now we try it dynamically
  cn &lt;-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
  </code></pre>
  
  <p>I suppose <code>mget()</code> is the solution, but I know too less about scoping to figure it out.</p>
  
  <p>Thanks! JBJ</p>
  
  <hr>
  
  <p><strong>Update: Solution</strong></p>
  
  <p>based on the answer by BondedDust</p>
  
  <pre><code>    D[, "new" := foo(fix,sapply(cn, function(x) {get(x)})), by=id]
  </code></pre>