I have data such as this:
dat <- mtcars %>% mutate(cyl2 = cyl*2,cyl3 = cyl*3)
I would like to run each of the following cross tabs [vs,cyl] [vs,cyl1] [vs,cyl2] [vs,cyl3] using tabyl:
I know that I can run vs, cyl such as this, and repeat this operation for each of the 'cyl' variable. However I would like to form some kind of loop instead of repeating this.
dat%>%
tabyl(vs,cyl)%>%
adorn_percentages("row") %>%
adorn_pct_formatting(digits = 2) %>%
adorn_ns()
So I worked on a function:
run_xtable <- function(data,v1) {
out <- data%>%
tabyl(vs,v1)%>%
adorn_percentages("row") %>%
adorn_pct_formatting(digits = 2) %>%
adorn_ns()
return(out)
}
run_xtable(dat,'cyl')
I have run into some issues, any help is much appreciated!!
The function is not accepting v1 as a reference variable. Why is this? I tried wrapping it in enquo, but no difference was made.
Error: Must group by variables found in
.data
.* Columnv1
is not found.How do I set this up so that I can use something like this to reduce repetition:
sapply(run_xtable, c('cyl','cyl1','cyl2'))
Thank you!