These scoped variants of group_by() group a data frame by a selection of variables. Like group_by(), they have optional mutate semantics.

group_by_all(.tbl, .funs = list(), ...)

group_by_at(.tbl, .vars, .funs = list(), ..., .add = FALSE)

group_by_if(.tbl, .predicate, .funs = list(), ..., .add = FALSE)

Arguments

.tbl

A tbl object.

.funs

List of function calls generated by funs(), or a character vector of function names, or simply a function.

Bare formulas are passed to rlang::as_function() to create purrr-style lambda functions. Note that these lambda prevent hybrid evaluation from happening and it is thus more efficient to supply functions like mean() directly rather than in a lambda-formula.

...

Additional arguments for the function calls in .funs. These are evaluated only once, with explicit splicing.

.vars

A list of columns generated by vars(), or a character vector of column names, or a numeric vector of column positions.

.add

Passed to the add argument of group_by().

.predicate

A predicate function to be applied to the columns or a logical vector. The variables for which .predicate is or returns TRUE are selected. This argument is passed to rlang::as_function() and thus supports quosure-style lambda functions and strings representing function names.

Examples

# Group a data frame by all variables: group_by_all(mtcars)
#> # A tibble: 32 x 11 #> # Groups: mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb [32] #> mpg cyl disp hp drat wt qsec vs am gear carb #> * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 #> 2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 #> 3 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 #> 4 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 #> 5 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 #> 6 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 #> 7 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 #> 8 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 #> 9 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 #> 10 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 #> # ... with 22 more rows
# Group by variables selected with a predicate: group_by_if(iris, is.factor)
#> # A tibble: 150 x 5 #> # Groups: Species [3] #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> <dbl> <dbl> <dbl> <dbl> <fctr> #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3.0 1.4 0.2 setosa #> 3 4.7 3.2 1.3 0.2 setosa #> 4 4.6 3.1 1.5 0.2 setosa #> 5 5.0 3.6 1.4 0.2 setosa #> 6 5.4 3.9 1.7 0.4 setosa #> 7 4.6 3.4 1.4 0.3 setosa #> 8 5.0 3.4 1.5 0.2 setosa #> 9 4.4 2.9 1.4 0.2 setosa #> 10 4.9 3.1 1.5 0.1 setosa #> # ... with 140 more rows
# Group by variables selected by name: group_by_at(mtcars, vars(vs, am))
#> # A tibble: 32 x 11 #> # Groups: vs, am [4] #> mpg cyl disp hp drat wt qsec vs am gear carb #> * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> #> 1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 #> 2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 #> 3 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 #> 4 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 #> 5 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 #> 6 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 #> 7 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 #> 8 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 #> 9 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 #> 10 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 #> # ... with 22 more rows
# Like group_by(), the scoped variants have optional mutate # semantics. This provide a shortcut for group_by() + mutate(): group_by_all(mtcars, as.factor)
#> # A tibble: 32 x 11 #> # Groups: mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb [32] #> mpg cyl disp hp drat wt qsec vs am gear carb #> <fctr> <fctr> <fctr> <fctr> <fctr> <fctr> <fctr> <fctr> <fctr> <fctr> <fctr> #> 1 21 6 160 110 3.9 2.62 16.46 0 1 4 4 #> 2 21 6 160 110 3.9 2.875 17.02 0 1 4 4 #> 3 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1 #> 4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 #> 5 18.7 8 360 175 3.15 3.44 17.02 0 0 3 2 #> 6 18.1 6 225 105 2.76 3.46 20.22 1 0 3 1 #> 7 14.3 8 360 245 3.21 3.57 15.84 0 0 3 4 #> 8 24.4 4 146.7 62 3.69 3.19 20 1 0 4 2 #> 9 22.8 4 140.8 95 3.92 3.15 22.9 1 0 4 2 #> 10 19.2 6 167.6 123 3.92 3.44 18.3 1 0 4 4 #> # ... with 22 more rows
group_by_if(iris, is.factor, as.character)
#> # A tibble: 150 x 5 #> # Groups: Species [3] #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> <dbl> <dbl> <dbl> <dbl> <chr> #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3.0 1.4 0.2 setosa #> 3 4.7 3.2 1.3 0.2 setosa #> 4 4.6 3.1 1.5 0.2 setosa #> 5 5.0 3.6 1.4 0.2 setosa #> 6 5.4 3.9 1.7 0.4 setosa #> 7 4.6 3.4 1.4 0.3 setosa #> 8 5.0 3.4 1.5 0.2 setosa #> 9 4.4 2.9 1.4 0.2 setosa #> 10 4.9 3.1 1.5 0.1 setosa #> # ... with 140 more rows