dplyr::group_by_ with character string input of several variable names

Solution 1:

No need for interp here, just use as.formula to convert the strings to formulas:

dots = sapply(y, . %>% {as.formula(paste0('~', .))})
mtcars %>% group_by_(.dots = dots)

The reason why your interp approach doesn’t work is that the expression gives you back the following:

~list(c("cyl", "gear"))

– not what you want. You could, of course, sapply interp over y, which would be similar to using as.formula above:

dots1 = sapply(y, . %>% {interp(~var, var = .)})

But, in fact, you can also directly pass y:

mtcars %>% group_by_(.dots = y)

The dplyr vignette on non-standard evaluation goes into more detail and explains the difference between these approaches.

Solution 2:

slice_rows() from the purrrlyr package (https://github.com/hadley/purrrlyr) groups a data.frame by taking a vector of column names (strings) or positions (integers):

y <- c("cyl", "gear")
mtcars_grp <- mtcars %>% purrrlyr::slice_rows(y)

class(mtcars_grp)
#> [1] "grouped_df" "tbl_df"     "tbl"        "data.frame"

group_vars(mtcars_grp)
#> [1] "cyl"  "gear"

Particularly useful now that group_by_() has been depreciated.