Programmatically dropping a `group_by` field in dplyr

前端 未结 3 1107
挽巷
挽巷 2021-02-20 05:49

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 g

3条回答
  •  再見小時候
    2021-02-20 06:29

    Function to remove groups by column name

    drop_groups_at <- function(df, vars){
      df %>% 
        group_by_at(setdiff(group_vars(.), vars))
    }
    
    
    input %>%
      group_by(a, b) %>% 
      drop_groups_at('b') %>% 
      group_vars
    
    # [1] "a"
    

提交回复
热议问题