Skip to content

Instantly share code, notes, and snippets.

Created May 27, 2019 18:31
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Apply multiple functions to multiple columns (with data.table)
my.summary = function(x) list(mean = mean(x), median = median(x))
DT[, as.list(unlist(lapply(.SD, my.summary))), .SDcols = c('a', 'b')]
Copy link

pbaylis commented May 27, 2019

N.B.: This can be slow for very large datasets – the as.list(unlist()) formulation is the culprit. Another formulation, which is a bit more code-heavy but better in terms of performance, is to melt the relevant columns and compute on the melted data.table.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment