cross_tab {spicy} | R Documentation |
Cross-Tabulation with Percentages, Weights, and Grouping
Description
cross_tab()
produces a cross-tabulation of x
by y
, with optional stratification using a grouping variable (by
).
It supports weighted frequencies, row or column percentages, and association statistics (Chi-squared test, Cramer's V).
Usage
cross_tab(
d = parent.frame(),
x,
y = NULL,
by = NULL,
weights = NULL,
rescale_weights = FALSE,
digits = 1,
rowprct = FALSE,
row_total = TRUE,
column_total = TRUE,
n = TRUE,
drop = TRUE,
include_stats = TRUE,
combine = FALSE,
...
)
Arguments
d |
A |
x |
Variable for table rows. Can be unquoted (tidy) or quoted (standard). Must match column name if |
y |
Optional variable for table columns. Same rules as |
by |
Optional grouping variable (or interaction of variables). Used to produce stratified crosstabs. Must refer to columns in |
weights |
Optional numeric vector of weights. Must match length of |
rescale_weights |
Logical. If |
digits |
Integer. Number of decimal places shown in percentages. Default is |
rowprct |
Logical. If |
row_total |
Logical. If |
column_total |
Logical. If |
n |
Logical. If |
drop |
Logical. If |
include_stats |
Logical. If |
combine |
Logical. If |
... |
Additional arguments passed to |
Details
The function is flexible:
Accepts both standard (quoted) and tidy (unquoted) variable input
Performs stratified tabulations using a grouping variable (
by
)Optionally combines group-level tables into a single tibble with
combine = TRUE
Pipe-friendly with both base R (
|>
) and magrittr (%>%
)
All variables (x
, y
, by
, weights
) must be present in the data frame d
(unless vector input is used).
Value
A tibble of class spicy
, or a list of such tibbles if combine = FALSE
and by
is used.
Warnings and Errors
If
weights
is non-numeric, an error is thrown.If
weights
does not match the number of observations, an error is thrown.If
rescale_weights = TRUE
but no weights are provided, a warning is issued.If all values in
by
areNA
, an error is thrown.If
by
has only one unique level (or allNA
), a warning is issued.
Examples
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)