I am looking to run a couple of dozen crosstabs within the same dataset and with a set outcome variable. I have a function that gives me the crosstabs I want:
second_table = function(dat, variable1, variable2){
dat %>%
tabyl({{variable1}}, {{variable2}}, show_na = FALSE) %>%
adorn_percentages("row") %>%
adorn_pct_formatting(digits = 1) %>%
adorn_ns()
}
Using the mtcars dataset as an example, the function gives me what I want for a single variable:
cars = datasets::mtcars
second_table(cars, cyl, vs)
What I really want, though, is to create lots of these tables where the dat = cars and variable2 = vs arguments stay the same, but using several different columns as the variable1 argument. For the purposes of this example, say it's the following 4 variables:
variables = c("cyl", "am", "gear", "carb")
I'm not sure if a map function from the purrr package is the best way to do this, but I've been unsuccessfully trying all sorts of different things with map and related functions like map_at. If there is a way to do this with purrr then that's what I'd prefer to do, but I'm open to any suggestions. I don't really care what the output looks like, just that I can get all the crosstabs I need without copying and pasting code lots of times.
Any help is greatly appreciated!