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