How can I write a function while working with tidyverse?

rstudio

#1

Hi All,

I am working with a data set, let's call it "mydata1" where I have two variables "gender" (levels: female and male), and EnteredARC (a binary variable whether one has entered a certain place with values: 0 & 1). Using tidyverse, I do something like as follows:

library(tidyverse)
gendervsentry <- mydata1 %>% # create a new data frame
  count(gender, EnteredARC) %>% # count entry against sex then
  spread(EnteredARC, n) 

This produces a nice table.

But if I were to write a function using the following arguments and call that function,

bivariate_table <- function(., x,y){
xy = count(x,y) %>%
        spread(y, n)
return(xy)
}

.. and then call,

mydata1 %>%
   bivariate_table(gender, EnteredARC)

I get the following error:

Error in bivar_table(., gender, EnteredARC) : unused argument (EnteredARC)

Or, if I do something like:

bivar_table <- function(data, x,y){
  xy = data %>%
        count(x,y) %>%
         spead(y, n)
  return(xy)
}

mydata1 %>%
  bivar_table(gender, EnteredARC )

I get the following error message:

"Error in grouped_df_impl(data, unname(vars), drop) : Column x is unknown"

What am I doing wrong?
How can I write a function and use it with dplyr?

Best,
Arin


#2

Hi,

you need to use tidyeval with quosure and quasiquotation. To program with dplyr you need enquo then !! (bang bang).

You should read the dplyr vignette

and watch those webinars and videos

This will help you understand.

An example from the vignette

my_summarise <- function(df, group_var) {
  group_var <- enquo(group_var)
  print(group_var)

  df %>%
    group_by(!! group_var) %>%
    summarise(a = mean(a))
}

my_summarise(df, g1)
#> <quosure>
#>   expr: ^g1
#>   env:  global
#> # A tibble: 2 x 2
#>      g1     a
#>   <dbl> <dbl>
#> 1     1  2.5 
#> 2     2  3.33