Created
May 27, 2019 18:31
-
-
Save pbaylis/3b736f67aa239ba993b9674f5b5496bc to your computer and use it in GitHub Desktop.
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.