dplyr - groupby on multiple columns using variable names

前端 未结 4 963
长发绾君心
长发绾君心 2020-12-08 07:33

I am working with R Shiny for some exploratory data analysis. I have two checkbox inputs that contain only the user-selected options. The first checkbox input contains only

相关标签:
4条回答
  • 2020-12-08 07:43

    You can use the helpers from rlang package, which is created by the same team that created dplyr. When using dplyr and other tidyverse packages, you don't have to load the rlang packages in order to use those helpers.

    Specifically, you can use the syms function and the !!! function like so:

    library(dplyr)
    
    group_cols <- c("vs", "am")
    
    mtcars %>% 
      group_by(!!!syms(group_cols)) %>% 
      summarize(mean_wt = mean(wt))
    

    This closely-related question and answer explains how the !! operator and sym function are used for a single column name (i.e. a length-one character vector).

    0 讨论(0)
  • 2020-12-08 07:52

    With dplyr 1.0.0, we have the following possibility based on the "normal" group_by:

    library(dplyr)
    
    group_cols <- c("vs", "am")
    
    mtcars %>% 
      group_by(across(all_of(group_cols))) %>% 
      summarize(mean_wt = mean(wt))
    
    0 讨论(0)
  • 2020-12-08 07:58

    If you have a vector of variable names, you should pass them to the .dots= parameter of group_by_. For example:

    mtcars %>% 
       group_by_(.dots=c("mpg","hp","wt")) %>% 
       summarize(x=mean(gear))
    
    0 讨论(0)
  • 2020-12-08 07:58

    Recent versions of the dplyr package include variants of group_by, such as group_by_if and group_by_at. You can use these to perform column selections with syntax that is similar to the select function.

    Just as you could select a list of columns with select(my_data, one_of(group_cols)), you can use group_by_at to do the following:

    library(dplyr)
    
    group_cols <- c("vs", "am")
    
    mtcars %>% 
      group_by_at(.vars = vars(one_of(group_cols))) %>% 
      summarize(mean_wt = mean(wt))
    
    0 讨论(0)
提交回复
热议问题