I have seen examples of using .SD
with lapply
in data.table
with a simple function as below:
DT[ , .(b,d,e) := lapply(.SD, tan), .SDcols = .(b,d,e)]
But I'm unsure of how to use column-specific arguments in a multiple argument function. For instance I have a winsorize
function, I want to apply it to a subset of columns in a data table but using column-specific percentiles, e.g.
library(DescTools)
wlevel <- list(b=list(lower=0.01,upper=0.99), c=list(upper=0.02,upper=0.95))
DT[ , .(b,c) :=lapply(.SD, function(x)
{winsorize(x,wlevel$zzz$lower,wlevel$zzz$upper)}), .SDcols = .(b,c)]
Where zzz
will be the respective column to iterate. I have also seen threads on using changing arguments with lapply
but not in the context of data table with .SDcols
Is this possible to do?
This is a toy example, looking to generalize for the case of arbitrary large number of columns; Looping is always an option but trying to see if there's a more elegant/efficient solution...