I'm making two tables that include lots of variables, so am looking to write functions that use tabyl() from the janitor package and map over the variables I'm interested in.
The first function works fine:
cars = datasets::mtcars
first_table = function(variable){
tabyl(variable, show_na = FALSE) %>%
adorn_pct_formatting(digits = 1)
}
first_table(cars$vs)
The second table is produced in almost the same way, but should produce the cross-tabs of two variables and row percents instead of a table representing a single variable. This code and output represents what I'm trying to do with my function:
cars %>%
tabyl(vs, am, show_na = FALSE) %>%
adorn_percentages("row") %>%
adorn_pct_formatting(digits = 1) %>%
adorn_ns()
When I write this as a function, however, it seems like the tabyl() function only wants to recognize the first variable:
second_table = function(variable1, variable2){
tabyl(variable1, variable2, show_na = FALSE) %>%
adorn_percentages("row") %>%
adorn_pct_formatting(digits = 1) %>%
adorn_ns()
}
second_table(cars$vs, cars$am)
I'm not quite sure what the issue is, and am wondering how I can edit this function to give me the 2x2 table with row percents that I was able to produce without a function above.
Any help is greatly appreciated.


