Pass data.table column to function when using .SDcols

I would like to use the syntax DT[, lapply(.SD, func), by=group, .SDcols=cols]

in data.table

, but I would like to pass a column from DT

to func()

. Is there a way to make this 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")]

      

+3


source to share


1 answer


I think the strategy here necessarily entails bad programming, but

DT[,lapply(
  .SD[,"value",with=FALSE], 
  indexfunc,indexcol= indexkey,indexvalue= 2
), by=group]

      

gives a way out



   group value
1:     A  0.50
2:     A  1.00
3:     B  0.75
4:     B  1.00

      

The OP's approach doesn't work because it .SDcols

limits the set of columns available in j

DT[i,j]

. I think the function arguments used in lapply

should be named as well.

+3


source







All Articles