Greg Y. answered 07/01/26
CS Degree with 5 Years of STEM Tutoring & 5 Years of Professional R
Tidyselection
One approach is using tidyselect syntax1. Specifically, to select columns whose names are in a string vector, we use the all_of() helper2.
Some functions like select()3 accept our tidyselection directly...
...but group_by()4 does not, so we must wrap it in across()5.
Quasiquotation
Another approach is using quasiquotation6 to build our code dynamically. Here we convert the column names into a list of symbols7, which we "splice" into our group_by() using the !!! operator8.
This behaves the same as hard-coding the columns by hand, but it is much more convenient.
1 https://tidyselect.r-lib.org/reference/language
2 https://tidyselect.r-lib.org/reference/all_of
3 https://dplyr.tidyverse.org/reference/select
4 https://dplyr.tidyverse.org/reference/group_by
5 https://dplyr.tidyverse.org/reference/across
6 https://adv-r.hadley.nz/quasiquotation
7 https://adv-r.hadley.nz/expressions#symbols
8 https://adv-r.hadley.nz/quasiquotation#unquoting-many-arguments