matrix_table {surveyexplorer}R Documentation

Create a table of frequencies and counts for matrix questions

Description

This function creates a table showing percentages and counts for each response option in a multiple-choice question, specified by question. If grouping is provided with group_by, the table is extended to include subgroups. Subgroups can be excluded, and survey weights are supported for adjusted counts. The table is formatted for clarity and can be displayed in wide format. When weights are used, counts are presented as percentages only, and a note is added at the bottom of the table.

Usage

matrix_table(
  dataset,
  question,
  group_by = NULL,
  subgroups_to_exclude = NULL,
  weights = NULL,
  na.rm = FALSE,
  column_order = NULL
)

Arguments

dataset

The input dataframe (or tibble) of survey questions

question

The columns that contain each of the response options for a question, can be selected by using tidyselect semanatics or providing a vector of column names or numbers

group_by

Optional variable to group the analysis. If provided, the frequencies and counts will be calculated within each subgroup.

subgroups_to_exclude

Optional vector specifying subgroups to exclude from the analysis.

weights

Optional variable containing survey weights. If provided, frequencies and counts will be weighted accordingly.

na.rm

Logical indicating whether to remove NA values from question before analysis.

column_order

reorder columns of final table with an argument to pass to dplyr::relocate()

Value

A gt table summarizing percentages and counts for each response option in the specified multiple-choice question. If grouping is provided, the table includes subgroups and is formatted for clarity.

@examples #Array question (1-5) matrix_table(berlinbears, dplyr::starts_with('p_'))

#Use group_by to partition the question into several groups matrix_table(berlinbears, dplyr::starts_with('p_'), group_by = species, subgroups_to_exclude = 'panda bear' )

#Remove NA category matrix_table(berlinbears, dplyr::starts_with('p_'), group_by = species, subgroups_to_exclude = 'panda bear', na.rm = TRUE

#Categorical input matrix_table(berlinbears, dplyr::starts_with('c_'), group_by = is_parent)

See Also

Other matrix questions: matrix_freq(), matrix_likert(), matrix_mean()


[Package surveyexplorer version 0.2.0 Index]