Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sharlagelfand/d34107da3af4e1a391101e72dbf63bb8 to your computer and use it in GitHub Desktop.
Save sharlagelfand/d34107da3af4e1a391101e72dbf63bb8 to your computer and use it in GitHub Desktop.
``` r
# tidyeval for multiple arguments that can take multiple variables
library(dplyr)
# in the case where the verbs' arguments are ... e.g. group_by(), select(), use !!! within the function to expand the variables back out
# no enquo() or enquos() is needed because you will pass in a vars() call
group_select <- function(df, group_vars, select_vars){
df %>%
group_by(!!!group_vars) %>%
select(!!!select_vars)
}
# when you call the function, pass vars() to each argument
mtcars %>%
group_select(group_vars = vars(cyl, gear), select_vars = vars(mpg, hp))
#> Adding missing grouping variables: `cyl`, `gear`
#> # A tibble: 32 x 4
#> # Groups: cyl, gear [8]
#> cyl gear mpg hp
#> * <dbl> <dbl> <dbl> <dbl>
#> 1 6 4 21 110
#> 2 6 4 21 110
#> 3 4 4 22.8 93
#> 4 6 3 21.4 110
#> 5 8 3 18.7 175
#> 6 6 3 18.1 105
#> 7 8 3 14.3 245
#> 8 4 4 24.4 62
#> 9 4 4 22.8 95
#> 10 6 4 19.2 123
#> # … with 22 more rows
# (do you really need a group_select() function? probably not, this is just an example where both verbs take ... arguments)
# in the case when the verb's argument is .vars and *already expects a list of columns generated by vars()*, you don't need to use !!!
group_mean <- function(df, group_vars, mean_vars){
df %>%
group_by(!!!group_vars) %>%
summarise_at(mean_vars, mean)
}
# pass the arguments in the same way, using vars()
mtcars %>%
group_mean(group_vars = vars(cyl, gear), mean_vars = vars(mpg, hp))
#> # A tibble: 8 x 4
#> # Groups: cyl [?]
#> cyl gear mpg hp
#> <dbl> <dbl> <dbl> <dbl>
#> 1 4 3 21.5 97
#> 2 4 4 26.9 76
#> 3 4 5 28.2 102
#> 4 6 3 19.8 108.
#> 5 6 4 19.8 116.
#> 6 6 5 19.7 175
#> 7 8 3 15.0 194.
#> 8 8 5 15.4 300.
# i think if you are just passing ONE variable in though, you still need to use vars() for both, e.g.
mtcars %>%
group_select(group_vars = vars(cyl), select_vars = vars(mpg))
#> Adding missing grouping variables: `cyl`
#> # A tibble: 32 x 2
#> # Groups: cyl [3]
#> cyl mpg
#> * <dbl> <dbl>
#> 1 6 21
#> 2 6 21
#> 3 4 22.8
#> 4 6 21.4
#> 5 8 18.7
#> 6 6 18.1
#> 7 8 14.3
#> 8 4 24.4
#> 9 4 22.8
#> 10 6 19.2
#> # … with 22 more rows
mtcars %>%
group_mean(group_vars = vars(cyl), mean_vars = vars(mpg))
#> # A tibble: 3 x 2
#> cyl mpg
#> <dbl> <dbl>
#> 1 4 26.7
#> 2 6 19.7
#> 3 8 15.1
# otherwise you would have to use the typical enquo() and !! for un-vars()'d variables, e.g. this totally fails
mtcars %>%
group_mean(group_vars = cyl, mean_vars = mpg)
#> Error in quos(...): object 'cyl' not found
```
<sup>Created on 2019-02-09 by the [reprex package](https://reprex.tidyverse.org) (v0.2.1)</sup>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment