I'd like to (dynamically and in place) bring some columns from one data table into another. I have the following code which gives the desired output:
# Set up example data
dt <- as.data.table(iris)[, rowid:=.I]
dt1 <- dt[, .(rowid, Sepal.Length, Sepal.Width)]
dt2 <- dt[, .(rowid, Species)]
cols <- c('Sepal.Length', 'Sepal.Width')
# Join the datasets
dt2[dt1, (cols):= mget(cols), on='rowid', verbose=T]
However, this is slow on my real data, and the output from verbose states: "'(m)get' found in j. ansvars being set to all columns. Use .SDcols or a single j=eval(macro) instead. Both will detect the columns used which is important for efficiency."
I've tried various forms of .SDcols and eval, but they all either give errors or give inappropriate output (e.g. columns contain the column name as a string). e.g.:
dt2[dt1, (cols):= .SD, .SDcols=cols, on='rowid', verbose=T]
dt2[dt1, (cols):= eval(quote("list(Sepal.Length, Sepal.Width)")), on='rowid', verbose=T]
dt2[dt1, (cols):= eval(parse(text="list(Sepal.Length, Sepal.Width)")), on='rowid', verbose=T]
NB. This question is similar to .SD and .SDcols for the i expression in data.table join , however the accepted answer in that case is to use mget, and I'm trying to find an alternative to that which is more efficient.
