Group a data frame by the groupingVars and compute user summaries on this data frame (user summaries specified in ...). Enforces the good dplyr pipeline design principle of keeping group_by and mutate close together. Author: John Mount, Win-Vector LLC.

group_mutate(d, groupingVars, ..., arrangeTerms = NULL, env = parent.frame())

Arguments

d

data.frame

groupingVars

character vector of column names to group by.

...

list of dplyr::mutate() expressions.

arrangeTerms

character optional vector of quoted column expressions to arrange by.

env

environment to work in.

Value

d mutateed by groups

Examples

group_mutate(datasets::mtcars, c("cyl", "gear"), group_mean_mpg = mean(mpg), group_mean_disp = mean(disp)) %.>% head(.)
#> # A tibble: 6 x 13 #> mpg cyl disp hp drat wt qsec vs am gear carb #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4 #> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4 #> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 #> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1 #> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2 #> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1 #> # … with 2 more variables: group_mean_mpg <dbl>, group_mean_disp <dbl>
group_mutate(datasets::mtcars, c("cyl", "gear"), rank = dplyr::row_number(), arrangeTerms = "-disp") %.>% head(.)
#> # A tibble: 6 x 12 #> mpg cyl disp hp drat wt qsec vs am gear carb rank #> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int> #> 1 10.4 8 472 205 2.93 5.25 18.0 0 0 3 4 1 #> 2 10.4 8 460 215 3 5.42 17.8 0 0 3 4 2 #> 3 14.7 8 440 230 3.23 5.34 17.4 0 0 3 4 3 #> 4 19.2 8 400 175 3.08 3.84 17.0 0 0 3 2 4 #> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2 5 #> 6 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4 6