| cross_tab | R Documentation |
Computes a two-way cross-tabulation with optional weights, grouping (including combinations of multiple variables), percentage displays, and inferential statistics.
cross_tab() produces weighted or unweighted contingency tables with
row or column percentages, optional grouping via by, and associated
Chi-squared tests with Cramer's V and diagnostic information.
Both x and y variables are required. For one-way frequency tables,
use freq() instead.
cross_tab(
data,
x,
y = NULL,
by = NULL,
weights = NULL,
rescale = FALSE,
percent = c("none", "column", "row"),
include_stats = TRUE,
correct = FALSE,
simulate_p = FALSE,
simulate_B = 2000,
digits = NULL,
styled = TRUE,
show_n = TRUE
)
## S3 method for class 'spicy_cross_table_list'
print(x, ...)
data |
A data frame. Alternatively, a vector when using the vector-based interface. |
x |
Row variable (unquoted). |
y |
Column variable (unquoted). Mandatory; for one-way tables, use |
by |
Optional grouping variable or expression. Can be a single variable
or a combination of multiple variables (e.g. |
weights |
Optional numeric weights. |
rescale |
Logical. If |
percent |
One of |
include_stats |
Logical. If |
correct |
Logical. If |
simulate_p |
Logical. If |
simulate_B |
Integer. Number of replicates for Monte Carlo simulation.
Defaults to |
digits |
Number of decimals. Defaults to |
styled |
Logical. If |
show_n |
Logical. If |
... |
Additional arguments passed to individual print methods. |
A data.frame, list of data.frames, or spicy_cross_table object.
When by is used, returns a spicy_cross_table_list.
The function recognizes the following global options that modify its default behavior:
options(spicy.percent = "column")
Sets the default percentage mode for all calls to cross_tab().
Valid values are "none", "row", and "column".
Equivalent to setting percent = "column" (or another choice) in each call.
options(spicy.simulate_p = TRUE)
Enables Monte Carlo simulation for all Chi-squared tests by default.
Equivalent to setting simulate_p = TRUE in every call.
options(spicy.rescale = TRUE)
Automatically rescales weights so that total weighted N equals the raw N.
Equivalent to setting rescale = TRUE in each call.
These options are convenient for users who wish to enforce consistent behavior
across multiple calls to cross_tab() and other spicy table functions.
They can be disabled or reset by setting them to NULL:
options(spicy.percent = NULL, spicy.simulate_p = NULL, spicy.rescale = NULL).
Example:
options(spicy.simulate_p = TRUE, spicy.rescale = TRUE) cross_tab(mtcars, cyl, gear, weights = mtcars$mpg)
# Basic crosstab
cross_tab(mtcars, cyl, gear)
# Weighted (rescaled)
cross_tab(mtcars, cyl, gear, weights = mtcars$mpg, rescale = TRUE)
# Grouped
cross_tab(mtcars, cyl, gear, by = am)
# Grouped by an interaction
cross_tab(mtcars, cyl, gear, by = interaction(vs, am))
# Vector interface
cross_tab(mtcars$cyl, mtcars$gear, percent = "c")
# Set default percent mode globally
options(spicy.percent = "column")
# Now this will display column percentages by default
cross_tab(mtcars, cyl, gear)
# Reset to default behavior
options(spicy.percent = NULL)
# 2x2 table with Yates correction
cross_tab(mtcars, vs, am, correct = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.