
Create a table or crosstab for a multiple response (pick-any) set
Source:R/02_03_tabulation_tab_mr.R
tab_mr.RdThis function produces a table or a crosstab for multiple response (pick-any) set questions.
If only
xis specified, the output is a table of the items in the multiple response set.If both
xandyare specified, the output is a crosstab of the multiple response set byy.Use the
weightargument to produce weighted tables; if omitted, results are unweighted.Set
prop = TRUE(default) to output proportions, orprop = FALSEto output frequenciesUse the
totalargument to add a total column whenyis specified. Ignored for one-way tables.Use the
sortargument (descorasc) to order rows. Setsort = NULLfor no sorting.Use the
roundargument to control decimal precision in percentage tables.If
numeric = TRUE, the function removes the base row and coerces values to numeric, making the output math-ready.
Supported coding schemes for multiple response items include:
Unchecked/CheckedNot Selected/Selected0/1
Usage
tab_mr(
data,
x,
y = NULL,
weight = NULL,
prop = TRUE,
total = TRUE,
sort = NULL,
round = 3,
numeric = FALSE
)Arguments
- data
A data frame containing the survey data.
- x
A variable prefix identifying the multiple response set (e.g.,
Q2forQ2_1,Q2_2, ...).- y
(Optional) A variable to cross-tabulate against the multiple response set. Defaults to
NULL.- weight
(Optional) A numeric weighting variable. If
NULL(default), results are unweighted.- prop
Logical; if
TRUE(default), outputs proportions. IfFALSE, outputs frequencies.- total
Logical; if
TRUE(default), adds a total column whenyis specified. Ignored for single-variable tables.- sort
Sorting order for rows:
desc,asc, orNULL(no sorting). Defaults toNULL.- round
Integer; number of decimal places for proportions. Defaults to
3.- numeric
Logical; if
TRUE, returns a data frame with numeric columns, with the n row removed. Defaults toFALSE.
Examples
# Unweighted proportions table of a multiple response set
tab_mr(data = survey_data, x = Q2)
#> Error: These packages are required but not installed: dplyr, tidyr, stringr, haven
# Weighted frequency crosstab of Q2 by gender, showing total column
tab_mr(data = survey_data, x = Q2, y = gender, weight = weight_var, prop = FALSE, total = TRUE)
#> Error: These packages are required but not installed: dplyr, tidyr, stringr, haven