group_by: Group by one or more variables

View source: R/group_by.R

group_byR Documentation

Group by one or more variables

Description

Determine the groups within a data.frame to perform operations on. ungroup() removes the grouping levels.

Usage

group_by(.data, ..., .add = FALSE, .drop = group_by_drop_default(.data))

ungroup(x, ...)

Arguments

.data

data.frame. The data to group.

...

One or more unquoted column names to group/ungroup the data by.

.add

logical(1). When FALSE (the default) group_by() will override existing groups. To add to existing groups, use .add = TRUE.

.drop

logical(1). Drop groups formed by factor levels that don't appear in the data? The default is TRUE except when .data has been previously grouped with .drop = FALSE. See group_by_drop_default() for details.

x

A data.frame.

Value

When using group_by(), a data.frame, grouped by the grouping variables.

When using ungroup(), a data.frame.

Examples

group_by(mtcars, am, cyl)
ungroup(mutate(group_by(mtcars, am, cyl), sumMpg = sum(mpg)))
mtcars %>%
  group_by(am, cyl) %>%
  mutate(sumMpg = sum(mpg)) %>%
  ungroup()
mtcars %>%
  group_by(carb) %>%
  filter(any(gear == 5))

# You can group by expressions: this is just short-hand for
# a mutate() followed by a group_by()
mtcars %>% group_by(vsam = vs + am)


poorman documentation built on Nov. 2, 2023, 5:27 p.m.