
Create a full distribution table for a grid question
Source:R/02_05_tabulation_tab_grid.R
tab_grid.RdThis function produces a table for grid survey questions, showing the full distribution across all response levels.
If only
xis specified, the output is a full-distribution table for each item in the grid question.Use the
weightargument to produce weighted tables; if omitted, results are unweighted.Set
prop = TRUE(default) to output proportions, orprop = FALSEto output frequencies.Use the
sortargument (ascordesc) to order rows based on top/bottom box scores defined bytop_bottom. Setsort = NULLfor no sorting.Use the
top_bottomargument (e.g.,T2,B3) to specify how many top or bottom levels to use when sorting grid items.Use the
roundargument to control decimal precision in proportions tables.If
numeric = TRUE, the function removes the base column and coerces values to numeric, making the output math-ready.
Usage
tab_grid(
data,
x,
weight = NULL,
prop = TRUE,
sort = NULL,
top_bottom = T2,
round = 3,
numeric = FALSE
)Arguments
- data
A data frame containing the survey data.
- x
A variable prefix identifying the grid question (e.g.,
Q3forQ3_1,Q3_2, ...).- weight
(Optional) A numeric weighting variable. If
NULL(default), results are unweighted.- prop
Logical; if
TRUE(default), outputs proportions. IfFALSE, outputs frequencies.- sort
Sorting order for rows:
desc,asc, orNULL(no sorting). Defaults toNULL.- top_bottom
A string of the form
TNorBNindicating how many top or bottom levels should be used for sorting rows.- 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 column removed. Defaults toFALSE.