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.795 0.784 #> 2 1 0.774 0.784 #> 3 2 0.923 0.704 #> 4 2 0.485 0.704 #> 5 3 0.500 0.500#> # A tibble: 5 x 3 #> g x x1 #> <dbl> <dbl> <dbl> #> 1 1 0.795 0.795 #> 2 1 0.774 0.795 #> 3 2 0.923 0.923 #> 4 2 0.485 0.923 #> 5 3 0.500 0.500#> # A tibble: 5 x 3 #> # Groups: g [3] #> g x x_mean #> <dbl> <dbl> <dbl> #> 1 1 0.795 0.695 #> 2 1 0.774 0.695 #> 3 2 0.923 0.695 #> 4 2 0.485 0.695 #> 5 3 0.500 0.695# 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.795 #> 2 0.774 #> 3 0.923 #> 4 0.485 #> 5 0.500