
Create a top/bottom box table or crosstab for a grid question
Source:R/02_07_tabulation_tab_grid_t.R
tab_grid_t.RdThis function produces a top- or bottom-box summary for grid survey questions.
If only
xis specified, the output is a top- or bottom-box table for each item in the grid question.If both
xandyare specified, the output is a crosstab of the selected top/bottom box byy.Use the
top_bottomargument (e.g.,T2,B3) to define how many top or bottom response levels are included in the box.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
totalargument to include a total column when ayvariable is specified. Ignored for one-way tables.Use the
sortargument (ascordesc) to order rows by their box score. Setsort = NULLfor no sorting.Use the
roundargument to control decimal precision in proportions tables.If
numeric = TRUE, the function removes the base row and coerces all values to numeric, making the output math-ready.
Usage
tab_grid_t(
data,
x,
y = NULL,
top_bottom = T2,
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 grid question (e.g.,
Q3forQ3_1,Q3_2, ...).- y
(Optional) A variable name to cross-tabulate against the grid question. Defaults to
NULL.- top_bottom
A string of the form
TNorBNindicating how many top or bottom levels to include in the box (e.g.,T2= top 2). Defaults toT2.- 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.- 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 base n row removed. Defaults toFALSE.
Examples
# Unweighted proportions table of a grid question, showing top 2 box
tab_grid_t(data = survey_data, x = Q4, top_bottom = T2)
#> Error: These packages are required but not installed: dplyr, tidyr, stringr, crayon
# Weighted proportions crosstab of a grid question by region, showing bottom 2 box
tab_grid_t(data = survey_data, x = Q4, y = region, top_bottom = B2, weight = weight_var)
#> Error: These packages are required but not installed: dplyr, tidyr, stringr, crayon