Programatically dropping a `group_by` field in dplyr

  • A+
Category:Languages

I'm writing functions that take in a data.frame and then do some operations. I need to add and subtract items from the group_by criteria in order to get where I want to go.

If I want to add a group_by criteria to a df, that's pretty easy:

library(tidyverse) set.seed(42) n <- 10 input <- data.frame(a = 'a',                      b = 'b' ,                      vals = 1 )  input %>%   group_by(a) ->  grouped   grouped #> # A tibble: 1 x 3 #> # Groups:   a [1] #>   a     b      vals #>   <fct> <fct> <dbl> #> 1 a     b        1.  ## add a group: grouped %>%    group_by(b, add=TRUE) #> # A tibble: 1 x 3 #> # Groups:   a, b [1] #>   a     b      vals #>   <fct> <fct> <dbl> #> 1 a     b        1.  ## drop a group? 

But how do I programmatically drop the grouping by b which I added, yet keep all other groupings the same?


Maybe something like this to remove grouping variables from the end of the list back:

grouped %>%   group_by(b, add=TRUE) -> grouped grouped %>% group_by_at(.vars = group_vars(.)[-2]) 

or use head or tail or something on the output from group_vars for more control.

It would be interesting to have this sort of utility function available more generally:

peel_groups <- function(.data,n){   .data %>%     group_by_at(.vars = head(group_vars(.data),-n)) } 

A more thought out version would likely include more careful checks on n being out of bounds.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: