See http://stackoverflow.com/questions/9315258/aggregating-sub-totals-and-grand-totals-with-data-table.
Last active
August 29, 2015 14:04
-
-
Save reinholdsson/8b6ea3b0ce31b22ad265 to your computer and use it in GitHub Desktop.
R data.table: Aggregated functions with totals
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
aggr_by <- function(data, j, ..., grand.total = T, total.label = "(all)", by = names(dplyr:::named_dots(...))) { | |
j = substitute(j) | |
# Calculate by each combination | |
lst <- lapply(1:length(by), function(i) { | |
x <- data[, eval(j), by = eval(by[1:i])] | |
if (i != length(by)) x[, (by[-(1:i)]) := total.label] | |
return(x) | |
}) | |
# Grand total | |
if (grand.total) lst <- c(lst, list(data[, eval(j)][, (by) := total.label])) | |
# Combine all tables | |
res <- rbindlist(lst, use.names = T, fill = F) | |
# Set proper column order | |
setcolorder(res, c(by, colnames(res)[!colnames(res) %in% by])) | |
# Sort values | |
setkeyv(res, by) | |
return(res) | |
} | |
## Example | |
# dt <- data.table::data.table(mtcars) | |
# aggr_by(dt, list(mean(disp), max(mpg)), vs, carb, gear) | |
# aggr_by(dt, list(A = mean(disp), B = any(hp > 120), C = .N), vs, carb, gear, total.label = "`") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment