R/add_group_summaries.R
add_group_summaries.Rd
Group a data frame by the groupingVars and compute user summaries on this data frame (user summaries specified in ...), then join these new columns back into the original data and return to the user. Author: John Mount, Win-Vector LLC.
add_group_summaries(d, groupingVars, ..., arrangeTerms = NULL)
d | data.frame |
---|---|
groupingVars | character vector of column names to group by. |
... | list of dplyr::mutate() expressions. |
arrangeTerms | character optional vector of column expressions to arrange by. |
d with grouped summaries added as extra columns
add_group_summaries(datasets::mtcars, c("cyl", "gear"), group_mean_mpg = mean(mpg), group_mean_disp = mean(disp)) %.>% head(.)#> mpg cyl disp hp drat wt qsec vs am gear carb group_mean_mpg #> 1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 19.750 #> 2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 19.750 #> 3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 26.925 #> 4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 19.750 #> 5 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 15.050 #> 6 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 19.750 #> group_mean_disp #> 1 163.8000 #> 2 163.8000 #> 3 102.6250 #> 4 241.5000 #> 5 357.6167 #> 6 241.5000