Skip to contents

Marks columns to group by for subsequent operations like summarise(). The grouping is stored as metadata and does not perform any computation until an aggregation is requested.

Usage

# S3 method for class 'tbl_gpu'
group_by(.data, ..., .add = FALSE, .drop = dplyr::group_by_drop_default(.data))

Arguments

.data

A tbl_gpu object created by tbl_gpu().

...

Variables to group by: column names (x, .data$x), a top-level dplyr::pick(), or computed columns (name = expression, or an unnamed expression such as a + 1; see "Computed groups" below). Like dplyr, tidyselect helpers are only allowed inside pick().

.add

If FALSE (default), will override existing groups. If TRUE, will add to existing groups.

.drop

Whether to drop unobserved factor-level combinations from group_data()/group_keys()/n_groups()/etc. Defaults to group_by_drop_default(.data): TRUE for an ungrouped table, the table's current setting for a grouped one (as in dplyr). See ".drop semantics" below for the exact scope of what's honored.

Value

A grouped tbl_gpu object. The object has the same data but with grouping columns recorded for use by summarise().

Details

group_by() itself does no GPU work: it records the grouping columns, and the verbs that follow (summarise(), mutate(), dplyr::filter(), the group_data() family, ...) then work group by group. Computed group columns (see below) are the exception: they run a mutate() first.

Computed groups

As in dplyr, a named argument (group_by(df, g = x %/% 2), or even a plain rename such as group_by(df, g = x)) is always computed like a mutate() expression: the original column (if any) is kept, a new (or overwritten) column is added under the given name, and the table is then grouped by that name. A later argument may use the column of an earlier one, as in mutate() (group_by(df, g = x %/% 2, h = g * 10)). An unnamed argument is a column reference (x, .data$x, .data[["x"]]), which names an existing column, a top-level pick(), which adds the columns it selects, or any other expression, which is computed into a new column named after the expression (group_by(df, a + 1) makes a column called a + 1). As soon as one argument is named or is not a column reference (a pick() or a NULL counts), every argument is data-masking, so a bare name that is not a column (a variable in the calling environment) is computed too, as in mutate(). All computed arguments run as one mutate() on the ungrouped data, in argument order, and the group columns come in the order the arguments were written. The old add = and .dots = arguments are defunct and error.

Calling group_by() with no arguments and .add = FALSE (the default) removes any grouping, as in dplyr.

.drop semantics

With .drop = FALSE, group_data() (and so group_keys(), n_groups() and the verbs that work per group) also has an empty group (.rows = integer(0)) for every combination of factor levels of the factor grouping columns that has no row. The combinations are expanded as in dplyr: under each parent group a factor column takes all its levels (plus NA if it occurs there) and a non-factor column the values that occur under that parent (a single NA under an empty parent).

The setting stays with the table through every verb that keeps a group column, and is reset to TRUE once no group column is left. As in dplyr, .drop defaults to the table's current setting, so regrouping a .drop = FALSE table keeps it.

Groups are recomputed from the data each time, and empty factor-level groups are re-added from the declared levels. Row-subsetting verbs also keep emptied groups of any key type, as dplyr does. On a .drop = FALSE table, filter(), filter_out(), slice(), slice_head(), slice_tail(), slice_min(), slice_max(), slice_sample(), semi_join(), anti_join() and rows_delete() keep every group, emptied ones included. .preserve = TRUE does the same for slice() and filter() on a .drop = TRUE table. The kept groups then show in group_data() and friends, collect(), and summarise(), count() and tally() (on the GPU, with a row for each emptied group). mutate(), select(), rename() and relocate() keep them as long as they leave the group columns alone. A verb that runs in dplyr on the CPU keeps whichever empty groups dplyr's own result kept (such a mutate() keeps them, such an arrange() on a .drop = TRUE table drops them). Every other verb, and any change of grouping, drops them.

See also

summarise.tbl_gpu for aggregating grouped data, ungroup.tbl_gpu for removing grouping, group_data.tbl_gpu for the grouping metadata query family

Examples

library(dplyr)
if (has_gpu()) {
  gpu_mtcars <- tbl_gpu(mtcars)

  # Group by a single column
  by_cyl <- gpu_mtcars |>
    group_by(cyl)

  # Group by multiple columns
  by_cyl_gear <- gpu_mtcars |>
    group_by(cyl, gear)

  # Computed group column
  by_half_cyl <- gpu_mtcars |>
    group_by(half_cyl = cyl %/% 2)

  # Use with summarise for aggregation
  result <- gpu_mtcars |>
    group_by(cyl) |>
    summarise(mean_mpg = mean(mpg)) |>
    collect()
}