Writing own function using dplyr and group_by - how to continue with changed column names

Viewed 798

I would like to make tables for publication that give the number of observations, grouped by two variables. The code for this works fine. However, I have run into problems when trying to turn this into a function.

I am using dplyr_0.7.2

Example using mtcars:

Code for table outside of function: this works

library(tidyverse) 

tab1 <- mtcars %>% count(cyl) %>% rename(Total = n) 

tab2 <- mtcars %>%
  group_by(cyl, gear) %>% count %>% 
  spread(gear, n)

tab <- full_join(tab1, tab2, by = "cyl")
tab


# This is the output (which is what I want)

A tibble: 3 x 5
cyl Total   `3`   `4`   `5`
<dbl> <int> <int> <int> <int>
1     4    11     1     8     2
2     6     7     2     4     1
3     8    14    12    NA     2

Trying to put this into a function

Function for tab1: this works

count_by_two_groups_A <- function(df, var1){
  var1 <- enquo(var1)
  tab1 <- df %>% count(!!var1) %>% rename(Total = n)
  tab1
} 

count_by_two_groups_A(mtcars, cyl) 

A tibble: 3 x 2
cyl Total
<dbl> <int>
1     4    11
2     6     7
3     8    14

Function for first part of tab2: it works up to this point, but...

count_by_two_groups_B <- function(df, var1, var2){

  var1 <- enquo(var1)
  var2 <- enquo(var2)

  tab2 <- df %>% group_by((!!var1), (!!var2)) %>% count
  tab2
} 

count_by_two_groups_B(mtcars, cyl, gear)

A tibble: 8 x 3
Groups:   (cyl), (gear) [8]
 `(cyl)` `(gear)`     n
 <dbl>    <dbl> <int>
 1       4        3     1
 2       4        4     8
 3       4        5     2
 4       6        3     2
 5       6        4     4
 6       6        5     1
 7       8        3    12
 8       8        5     2

The column names have changed to (cyl) and (gear). I can't seem to figure out how to carry on with spread() and full_join() (or anything else using the new column names) now that the column names have changed. I.e. I can't figure out how to specify the new column names in the tidyeval way, to be able to carry on. I have tried various things, without success.

3 Answers
Related