In the case where a tibble is grouped by multiple variables in dplyr
, is there a way to remove a single grouping variable other than re-specifying the groups without that variable? I'm thinking it would be something like group_by(df, -var, add = TRUE)
, though that doesn't work.
Example:
library(dplyr)
# Works
mtcars %>%
# Original groups
group_by(cyl, gear, carb) %>%
# New groups
group_by(cyl, gear) %>%
group_vars()
# [1] "cyl" "gear"
# Doesn't work
mtcars %>%
# Original groups
group_by(cyl, gear, carb) %>%
# New groups
group_by(-carb, add = TRUE) %>%
group_vars()
# [1] "cyl" "gear" "carb" "-carb"
This is clearly a trivial example - my actual use case has lots of conditional groupings based on user input and I'd like to just drop one grouping at some point in the function and leave the rest.