Apply multiple functions to multiple columns (with data.table)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
|my.summary = function(x) list(mean = mean(x), median = median(x))|
|DT[, as.list(unlist(lapply(.SD, my.summary))), .SDcols = c('a', 'b')]|
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.