Allow .SDcols to vary with grouping variable in data.table <p>Is it allowable to have <code>.SDcols</code> vary with the <code>by</code> grouping variable? I have the following situation, where I would like to change <code>.SDcols</code> to different columns for each year. The values for the <code>.SDcols</code> are in one data.table, while I am trying to apply a function to the <code>.SD</code> in another table using these values.</p> <p>Quite likely I am missing the obvious approach and doing this wrong, but this is what I was attempting,</p> <pre><code>## Contains the .SDcols applicable to each year dat1 &lt;- data.table( year = 1:4, vals = lapply(1:4, function(i) letters[1:i]) ) ## Make the sample data (with NAs) set.seed(1775) dat2 &lt;- data.table( year = sample(1:4, 10, TRUE) ) dat2[, letters[1:4] := replicate(4, sample(c(NA, 1:5), 10, TRUE), simplify=FALSE)] ## Goal: Sum up the columns in the corresponding .SDcols for each year ## Attempt, doesn't work -- I think b/c .SDcols must be fixed? dat2[, SUM := rowSums(.SD, na.rm=TRUE), by=year, .SDcols=unlist(dat1[year == .BY[[1]], vals])] ## Desired result, by simply iterating through each possible year for (i in 1:4) { dat2[year==i, SUM := rowSums(.SD, na.rm=TRUE), .SDcols=unlist(dat1[year == i, vals])] } dat2[] # year a b c d SUM # 1: 1 3 1 5 1 3 # 2: 2 1 3 3 1 4 # 3: 1 5 4 3 NA 5 # 4: 4 1 NA 4 5 10 # 5: 2 2 2 2 NA 4 # 6: 2 NA 3 3 NA 3 # 7: 4 2 3 2 NA 7 # 8: 1 2 NA 5 4 2 # 9: 2 3 3 5 1 6 # 10: 3 NA 4 2 NA 6 </code></pre>