Use column index instead of name in group_by

后端 未结 4 1695
陌清茗
陌清茗 2021-01-11 23:29

I want to summarize a dataframe with dplyr, like so:

> test <-data.frame(ID = c(\"A\", \"A\", \"B\", \"B\"), val = c(1:4))
> test %>% group_by(ID         


        
相关标签:
4条回答
  • 2021-01-12 00:03

    In older versions of dpylyr, You could use standard evaluation with dplyr::group_by_:

    test %>% 
     group_by_(names(.)[1]) %>% 
     summarize(av = mean(val))
    ## A tibble: 2 x 2
    #      ID    av
    #  <fctr> <dbl>
    #1      A   1.5
    #2      B   3.5
    
    0 讨论(0)
  • 2021-01-12 00:11

    You can use one of the scoped variants (group_by_at) for this:

    test %>% group_by_at(1) %>% summarise(av = mean(val))
    
    # A tibble: 2 x 2
    #      ID    av
    #  <fctr> <dbl>
    #1      A   1.5
    #2      B   3.5
    
    0 讨论(0)
  • 2021-01-12 00:11

    If we need to use NSE, then sym and !! can be used

    test %>%
         group_by(!! rlang::sym(names(.)[1])) %>%
         summarise(av = mean(val))
    # A tibble: 2 x 2
    #      ID    av
    #  <fctr> <dbl>
    #1      A   1.5
    #2      B   3.5
    

    We can also create a function. If we pass quoted strings, then we use sym with !! or else go for the enquo/!! route

    f1 <- function(dat, grp, valueCol) {
         dat %>%
            group_by(!! rlang::sym(grp)) %>%
            summarise(av = mean(!! rlang::sym(valueCol)))
    }
    
    f1(test, "ID", "val")
    # A tibble: 2 x 2
    #      ID    av
    #  <fctr> <dbl>
    #1      A   1.5
    #2      B   3.5
    
    0 讨论(0)
  • 2021-01-12 00:12

    You can use the across functionality as of version 1.0.0:

    library(dplyr)
    test %>% 
      group_by(across(1)) %>% 
      summarise(av = mean(val))
    ## A tibble: 2 x 2
    #  ID       av
    #  <fct> <dbl>
    #1 A       1.5
    #2 B       3.5
    
    0 讨论(0)
提交回复
热议问题