Use column index instead of name in group_by

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

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

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

Tags:

R

Dplyr