0
votes

I'm trying to write a function to make a long three way contingency table use group_by and summarize from dplyr. I start with a tibble containing 3 factor variables: year, mammogram and sp.

> c_table_fn <- function(x){
+ factors %>%
+ group_by(year,mammogram,x) %>%
+ summarize(n = n())}
> 
> c_table_fn(sp)

Error: Must group by variables found in `.data`.
* Column `x` is not found

My function gives me the above error. However, when I run the same code not in a function, it runs fine and I get the desired table.


> factors %>%
+ group_by(year,mammogram,sp) %>%
+ summarize(n = n())
`summarise()` has grouped output by 'year', 'mammogram'. You can override using the `.groups` argument.

I think this is due to an issue with tidy evaluation. I tried changing x in my function to {{x}} as well as .data[[x]] as suggested in the dplyr programming vignette, but this did not help.

1
Please provide a reproducible minimal example. Especially, provide some sample data, e.g. with dput() and use the reprex-package. - mnist

1 Answers

1
votes

We can use {{}}

c_table_fn <- function(x){
 factors %>%
 group_by(year,mammogram,{{x}}) %>%
 summarize(n = n())
}

-testing

 c_table_fn(sp)