Marius
Marius

Reputation: 60070

Passing column names through multiple functions with dplyr

I wrote a simple function to create tables of percentages in dplyr:

library(dplyr)

df = tibble(
    Gender = sample(c("Male", "Female"), 100, replace = TRUE),
    FavColour = sample(c("Red", "Blue"), 100, replace = TRUE)
)

quick_pct_tab = function(df, col) {
    col_quo = enquo(col)
    df %>%
        count(!! col_quo) %>%
        mutate(Percent = (100 * n / sum(n)))
}

df %>% quick_pct_tab(FavColour)
# Output:
# A tibble: 2 x 3
  FavColour     n Percent
      <chr> <int>   <dbl>
1      Blue    58      58
2       Red    42      42

This works great. However, when I tried to build on top of this, writing a new function that calculated the same percentages with grouping, I could not figure out how to use quick_pct_tab within the new function - after trying multiple different combinations of quo(col), !! quo(col) and enquo(col), etc.

bygender_tab = function(df, col) {
    col_enquo = enquo(col)
    # Want to replace this with 
    #   df %>% quick_pct_tab(col)
    gender_tab = df %>%
        group_by(Gender) %>%
        count(!! col_enquo) %>%
        mutate(Percent = (100 * n / sum(n)))

    gender_tab %>%
        select(!! col_enquo, Gender, Percent) %>%
        spread(Gender, Percent)
}
> df %>% bygender_tab(FavColour)
# A tibble: 2 x 3
  FavColour   Female     Male
*     <chr>    <dbl>    <dbl>
1      Blue 52.08333 63.46154
2       Red 47.91667 36.53846

From what I understand non-standard evaluation in dplyr is deprecated so it would be great to learn how to achieve this using dplyr > 0.7. How do I have to quote the col argument to pass it through to a further dplyr function?

Upvotes: 2

Views: 342

Answers (1)

akrun
akrun

Reputation: 887108

We need to do !! to trigger the evaluation of the 'col_enquo'

bygender_tab = function(df, col) {
   col_enquo = enquo(col)

   df %>% 
      group_by(Gender) %>%
      quick_pct_tab(!!col_enquo)  %>%  ## change
      select(!! col_enquo, Gender, Percent) %>%
      spread(Gender, Percent)   
}

df %>% 
    bygender_tab(FavColour)
# A tibble: 2 x 3
#   FavColour   Female     Male
#*     <chr>    <dbl>    <dbl>
#1      Blue 54.54545 41.07143
#2       Red 45.45455 58.92857

Using the OP's function, the output is

# A tibble: 2 x 3
#  FavColour   Female     Male
#*     <chr>    <dbl>    <dbl>
#1      Blue 54.54545 41.07143
#2       Red 45.45455 58.92857

Note that the seed was not set while creating the dataset

Update

with rlang version 0.4.0 (ran with dplyr - 0.8.2), we can also use the {{...}} to do quote, unquote, substitution

bygender_tabN = function(df, col) {
  

    df %>% 
       group_by(Gender) %>%
       quick_pct_tab({{col}})  %>%  ## change
       select({{col}}, Gender, Percent) %>%
       spread(Gender, Percent)   
 }
 
df %>% 
     bygender_tabN(FavColour)
# A tibble: 2 x 3
#  FavColour Female  Male
#  <chr>      <dbl> <dbl>
#1 Blue          50  46.3
#2 Red           50  53.7
     

-checking output with previous function (set.seed was not provided)

df %>% 
     bygender_tab(FavColour)
# A tibble: 2 x 3
#  FavColour Female  Male
#  <chr>      <dbl> <dbl>
#1 Blue          50  46.3
#2 Red           50  53.7

Upvotes: 2

Related Questions