Occasionally it is useful to ungroup a data frame before applying a calculation that would otherwise by slowed down by subdividing the calculation by group. In these circumstances, the calculation should be independent of the grouping. with_ungroup() temporarily removes groups, applies the function .f to .data (as .f(.data)) and then restores the original grouping. This function is fastest when the applied function does not modify the row order or the value of grouping columns. In these cases, the group index will need to be recalculated adding computational overhead depending on the number of rows and groups in the data.

with_ungroup(.data, .f, ...)

Arguments

.data

A grouped tbl, tibble, or data.frame

.f

A function, formula, or vector (not necessarily atomic).

If a function, it is used as is.

If a formula, e.g. ~ .x + 2, it is converted to a function. There are three ways to refer to the arguments:

  • For a single argument function, use .

  • For a two argument function, use .x and .y

  • For more arguments, use ..1, ..2, ..3 etc

This syntax allows you to create very compact anonymous functions.

If character vector, numeric vector, or list, it is converted to an extractor function. Character vectors index by name and numeric vectors index by position; use a list to index by position and name at different levels. If a component is not present, the value of .default will be returned.

...

Additional arguments passed on to methods.

See also

Other Group Utilities: group_drop, with_retain_groups

Examples

# with_ungroup() ungroups the input data frame, applies the inner function, # and restores grouping on output tidyr::table1 %>% dplyr::group_by(country, year) %>% with_ungroup(~ dplyr::mutate(., r = cases / population))
#> # A tibble: 6 x 5 #> # Groups: country, year [6] #> country year cases population r #> <chr> <int> <int> <int> <dbl> #> 1 Afghanistan 1999 745 19987071 0.0000373 #> 2 Afghanistan 2000 2666 20595360 0.000129 #> 3 Brazil 1999 37737 172006362 0.000219 #> 4 Brazil 2000 80488 174504898 0.000461 #> 5 China 1999 212258 1272915272 0.000167 #> 6 China 2000 213766 1280428583 0.000167
# Groups that "disappear" are implicitly dropped, with a warning tidyr::table1 %>% dplyr::group_by(country, year) %>% with_ungroup(~ { dplyr::mutate(., r = cases/population) %>% dplyr::select(-year) })
#> Warning: groups were implicitly dropped: `year`
#> # A tibble: 6 x 4 #> # Groups: country [3] #> country cases population r #> <chr> <int> <int> <dbl> #> 1 Afghanistan 745 19987071 0.0000373 #> 2 Afghanistan 2666 20595360 0.000129 #> 3 Brazil 37737 172006362 0.000219 #> 4 Brazil 80488 174504898 0.000461 #> 5 China 212258 1272915272 0.000167 #> 6 China 213766 1280428583 0.000167
# Works like "normal" if no groupings are present tidyr::table1 %>% with_ungroup(~ dplyr::mutate(., r = cases/population))
#> # A tibble: 6 x 5 #> country year cases population r #> <chr> <int> <int> <int> <dbl> #> 1 Afghanistan 1999 745 19987071 0.0000373 #> 2 Afghanistan 2000 2666 20595360 0.000129 #> 3 Brazil 1999 37737 172006362 0.000219 #> 4 Brazil 2000 80488 174504898 0.000461 #> 5 China 1999 212258 1272915272 0.000167 #> 6 China 2000 213766 1280428583 0.000167