I am writing a function to produce a frequency table using prop.table, and want to produce this for several categorical variables in the data set.
I am using datasets::mtcars for this example. I am looking to write the function with a group_by on the binary variable "am" in the dataset, so the output is stratified by am == 1 and am == 0.
For this code, is there a way to add a group_by statement?
summary(mtcars)
apply(mtcars[c("cyl", "gear", "carb")], 2,
\(x) prop.table(table(x, useNA = "always"))*100)
You can use
by()in base R: