I would like to use the DT[, lapply(.SD, func), by=group, .SDcols=cols]
syntax in a data.table
, but I would like to pass a column of DT
to func()
. Is there a way to get this to work? For example,
indexfunc <- function(col, indexcol, indexvalue)
col/col[indexcol==indexvalue]
DT <- data.table(group=c('A','A','B','B'), indexkey=c(1,2,1,2), value=1:4)
# Works
DT[, indexfunc(value, indexkey, 2), by=group]
# Fails, Error in indexfunc(value, indexkey, 2) : object 'indexkey' not found
DT[, lapply(.SD, indexfunc, indexkey, 2), by=group, .SDcols=c("value")]