This is an experimental new function that allows you to modify the grouping variables for a single operation.
with_groups(.data, .groups, .f, ...)
| .data | A data frame |
|---|---|
| .groups | < Use |
| .f | Function to apply to regrouped data.
Supports purrr-style |
| ... | Additional arguments passed on to |
#> # A tibble: 5 x 3 #> g x x_mean #> <dbl> <dbl> <dbl> #> 1 1 0.828 0.667 #> 2 1 0.507 0.667 #> 3 2 0.186 0.202 #> 4 2 0.218 0.202 #> 5 3 0.522 0.522#> # A tibble: 5 x 3 #> g x x1 #> <dbl> <dbl> <dbl> #> 1 1 0.828 0.828 #> 2 1 0.507 0.828 #> 3 2 0.186 0.186 #> 4 2 0.218 0.186 #> 5 3 0.522 0.522#> # A tibble: 5 x 3 #> # Groups: g [3] #> g x x_mean #> <dbl> <dbl> <dbl> #> 1 1 0.828 0.452 #> 2 1 0.507 0.452 #> 3 2 0.186 0.452 #> 4 2 0.218 0.452 #> 5 3 0.522 0.452# NB: grouping can't be restored if you remove the grouping variables df %>% group_by(g) %>% with_groups(NULL, mutate, g = NULL)#> # A tibble: 5 x 1 #> x #> <dbl> #> 1 0.828 #> 2 0.507 #> 3 0.186 #> 4 0.218 #> 5 0.522