with_groups {poorman} | R Documentation |
Perform an operation with temporary groups
Description
This function allows you to modify the grouping variables for a single operation.
Usage
with_groups(.data, .groups, .f, ...)
Arguments
.data |
A |
.groups |
< Use |
.f |
A |
... |
Additional arguments passed on to |
Examples
df <- data.frame(g = c(1, 1, 2, 2, 3), x = runif(5))
df %>% with_groups(g, mutate, x_mean = mean(x))
df %>% with_groups(g, ~ mutate(.x, x_mean = mean(x)))
df %>%
group_by(g) %>%
with_groups(NULL, mutate, x_mean = mean(x))
# NB: grouping can't be restored if you remove the grouping variables
df %>%
group_by(g) %>%
with_groups(NULL, mutate, g = NULL)
[Package poorman version 0.2.7 Index]