data(mtcars)
mtcars$gear <- factor(mtcars$gear)
mtcars$cyl <- factor(mtcars$cyl)
mtcars$vs <- factor(mtcars$vs, labels = c("V", "S"))
mtcars$am <- factor(mtcars$am, labels = c("auto", "manual"))
# Basic usage
cross_tab(mtcars, cyl, gear)
# Using extracted variables
cross_tab(mtcars$cyl, mtcars$gear)
# Pipe-friendly syntax
mtcars |> cross_tab(cyl, gear, by = am)
# With row percentages
cross_tab(mtcars, cyl, gear, by = am, rowprct = TRUE)
# Using weights
cross_tab(mtcars, cyl, gear, weights = mpg)
# With rescaled weights
cross_tab(mtcars, cyl, gear, weights = mpg, rescale_weights = TRUE)
# Grouped by a single variable
cross_tab(mtcars, cyl, gear, by = am)
# Grouped by interaction of two variables
cross_tab(mtcars, cyl, gear, by = interaction(am, vs), combine = TRUE)
# Combined output for grouped data
cross_tab(mtcars, cyl, gear, by = am, combine = TRUE)
# Without totals or sample size
cross_tab(mtcars, cyl, gear, row_total = FALSE, column_total = FALSE, n = FALSE)
Run the code above in your browser using DataLab