In order to speed up generating grouped summaries across multiple tables; as I\'m doing most of that while in dplyr workflow, I\'ve drafted a simple fun
Maybe use rename
:
library(tidyverse)
generate_summary_tbl <- function(dataset, group_column, summary_column, useColname = FALSE) {
group_column <- enquo(group_column)
summary_column <- enquo(summary_column)
dataset %>%
group_by(!!group_column) %>%
summarise(
mean = mean(!!summary_column),
sum = sum(!!summary_column)
# Other metrics that need to be generated frequently
) %>%
ungroup -> smryDta
if (useColname)
smryDta <- smryDta %>%
rename_at(
vars(-one_of(quo_name(group_column))),
~paste(quo_name(group_column), .x, sep="_")
)
return(smryDta)
}
mtcars %>% generate_summary_tbl(am, mpg)
# # A tibble: 2 x 3
# am mean sum
#
# 1 0 17.14737 325.8
# 2 1 24.39231 317.1
mtcars %>% generate_summary_tbl(am, mpg, T)
# # A tibble: 2 x 3
# am am_mean am_sum
#
# 1 0 17.14737 325.8
# 2 1 24.39231 317.1