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 data.frame, or a vector (when using vector input). Must contain all variables used in x, y, by, and weights.

x

Variable for table rows. Can be unquoted (tidy) or quoted (standard). Must match column name if d is a data frame.

y

Optional variable for table columns. Same rules as x. If NULL, computes a one-way frequency table.

by

Optional grouping variable (or interaction of variables). Used to produce stratified crosstabs. Must refer to columns in d, or be a vector of the same length as x.

weights

Optional numeric vector of weights. Must match length of x.

rescale_weights

Logical. If TRUE, rescales weights so that total weighted count matches unweighted count.

digits

Integer. Number of decimal places shown in percentages. Default is 1.

rowprct

Logical. If TRUE, computes percentages by row; otherwise by column.

row_total

Logical. If TRUE, adds row totals (default TRUE).

column_total

Logical. If TRUE, adds column totals (default TRUE).

n

Logical. If TRUE, displays effective counts N as an extra row or column (default TRUE).

drop

Logical. If TRUE, drops empty rows or columns (default TRUE).

include_stats

Logical. If TRUE, includes Chi-squared test and Cramer's V when possible (default TRUE).

combine

Logical. If TRUE, combines all stratified tables into one tibble with a by column.

...

Additional arguments passed to print.spicy(), such as show_all = TRUE

Details

The function is flexible:

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

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)


[Package spicy version 0.1.0 Index]