• group_by() adds a grouping structure to a tidytable. Can use tidyselect syntax.

  • ungroup() removes grouping.

group_by(.df, ..., .add = FALSE)

ungroup(.df, ...)

Arguments

.df

A data.frame or data.table

...

Columns to group by

.add

Should grouping cols specified be added to the current grouping

Examples

df <- data.table(
  a = 1:3,
  b = 4:6,
  c = c("a", "a", "b"),
  d = c("a", "a", "b")
)

df %>%
  group_by(c, d) %>%
  summarize(mean_a = mean(a)) %>%
  ungroup()
#> # A tidytable: 2 × 3
#>   c     d     mean_a
#>   <chr> <chr>  <dbl>
#> 1 a     a        1.5
#> 2 b     b        3  

# Can also use tidyselect
df %>%
  group_by(where(is.character)) %>%
  summarize(mean_a = mean(a)) %>%
  ungroup()
#> # A tidytable: 2 × 3
#>   c     d     mean_a
#>   <chr> <chr>  <dbl>
#> 1 a     a        1.5
#> 2 b     b        3