The multiple_response class is experimental. Its API may
change, and it may move to a different package.
One question, several answers
A multiple-response question lets respondents select more than one answer. Survey exports often store each answer in a separate selected/not-selected column. Summarizing those columns independently produces two rows per answer, rather than one coherent question.
collect_mr() combines those columns into a single
multiple_response column. srv_prop_stats()
then summarizes selections, with one row per response level.
data <- collect_mr(
example_srv_main,
news_sources = starts_with("m1_news_source_")
)
levels(data$news_sources)
#> [1] "Cable TV news" "Social media"
#> [3] "Podcasts" "Print or online newspapers"
head(data$news_sources)
#> <multiple_response> Module 1: News source
#> 6 respondents x 4 options
#> Cable TV news Social media Podcasts Print or online newspapers
#> [1,] TRUE TRUE TRUE TRUE
#> [2,] TRUE TRUE FALSE FALSE
#> [3,] TRUE FALSE TRUE FALSE
#> [4,] TRUE FALSE FALSE FALSE
#> [5,] FALSE TRUE FALSE FALSE
#> [6,] FALSE FALSE FALSE TRUEThe object holds a logical matrix: rows are respondents and columns
are response levels. TRUE means selected,
FALSE means not selected, and NA means
missing. It behaves like a vector of respondent answers:
data$news_sources[1] returns one respondent’s complete
row.
Summarize questions together
Select any combination of ordinary single-response (SR) columns and
collected multiple-response (MR) columns in one
srv_prop_stats() call. The function handles each question
according to its class. Here, news interest is an SR question and news
sources is an MR question.
source_stats <- srv_prop_stats(
data,
sbj_cols = c(m1_news_interest, news_sources),
weight_col = weight
)
source_stats
#> # A tibble: 8 × 7
#> sbj_col sbj_label sbj_level n N percentage question_N
#> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 m1_news_interest Module 1: Intere… Very int… 88.3 400 0.221 400
#> 2 m1_news_interest Module 1: Intere… Somewhat… 142. 400 0.354 400
#> 3 m1_news_interest Module 1: Intere… Not very… 99.6 400 0.249 400
#> 4 m1_news_interest Module 1: Intere… Not at a… 70.5 400 0.176 400
#> 5 news_sources Module 1: News s… Cable TV… 178. 400 0.445 400
#> 6 news_sources Module 1: News s… Social m… 238. 400 0.594 400
#> 7 news_sources Module 1: News s… Podcasts 138. 400 0.345 400
#> 8 news_sources Module 1: News s… Print or… 138. 400 0.345 400For the MR question, n is the weighted number selecting
a level. N is the weighted respondent base, including
people who did not select that level. Percentages can sum above 100%
because a respondent can select several levels. Levels with no
selections still appear with a count of zero.
With na.rm = TRUE, each level excludes its missing
answers from its base. With the default na.rm = FALSE,
missing answers remain in the base but do not count as selections. An
all-FALSE response is a valid answer with no
selections.
Compare cohorts and render a workbook
The same mixed selection works with cohorts. Here, both questions are summarized for each gender group and compared with the full sample.
cohort_set_gender <- g6tr.cohorts::create_cohort_set(
data,
id_col = response_id,
grp_cols = d_gender,
weight_col = weight
)
source_stats_gender <- srv_prop_stats(
data,
sbj_cols = c(m1_news_interest, news_sources),
cohorts = cohort_set_gender,
id_col = response_id
)
source_stats_gender |>
select(label, level, sbj_label, sbj_level, percentage, ref_percentage, index, pval)
#> # A tibble: 24 × 8
#> label level sbj_label sbj_level percentage ref_percentage index pval
#> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 Overall po… popu… Module 1… Very int… 0.221 0.221 100 NA
#> 2 Overall po… popu… Module 1… Somewhat… 0.354 0.354 100 NA
#> 3 Overall po… popu… Module 1… Not very… 0.249 0.249 100 NA
#> 4 Overall po… popu… Module 1… Not at a… 0.176 0.176 100 NA
#> 5 Overall po… popu… Module 1… Cable TV… 0.445 0.445 100 NA
#> 6 Overall po… popu… Module 1… Social m… 0.594 0.594 100 NA
#> 7 Overall po… popu… Module 1… Podcasts 0.345 0.345 100 NA
#> 8 Overall po… popu… Module 1… Print or… 0.345 0.345 100 NA
#> 9 Gender Male Module 1… Very int… 0.153 0.221 69.4 0.0614
#> 10 Gender Male Module 1… Somewhat… 0.299 0.354 84.4 0.197
#> # ℹ 14 more rows
wb <- srv_prop_stats_render(source_stats_gender)The workbook displays both questions, each in its own block with a
row per level. The MR total row uses the question’s respondent base
(question_N), not the sum of selections. When missing
answers are removed, this question base excludes respondents whose
entire MR answer is missing. Both question types use the same summary
and renderer.
Choosing question labels and levels
The defaults usually extract readable names without extra arguments.
They try the metadata assigned by g6tr.survey step
functions, then the longest common prefix of component labels, then
complete labels or source column names.
For example, component labels News sources | Television
and News sources | Radio become the question label
News sources and levels Television and
Radio.
Use explicit values when you want different wording:
data_short_labels <- collect_mr(
example_srv_main,
news_sources = starts_with("m1_news_source_"),
label = "News sources",
levels = c(
m1_news_source_1 = "Cable TV",
m1_news_source_2 = "Social media",
m1_news_source_3 = "Podcasts",
m1_news_source_4 = "Newspapers"
)
)Unnamed levels follow selection order; named levels match the original source column names. Names must match the complete selection, and resulting levels must be unique and nonempty.
Both arguments also accept functions. Each function receives a named
list of the original selected columns, including their attributes. A
label function returns one string or NULL; a levels
function returns one string per column. The two functions receive the
same original input.
The built-in helpers share one help page,
?collect_mr_label_default:
| Strategy | Label helper | Levels helper |
|---|---|---|
| Automatic chain | collect_mr_label_default() |
collect_mr_levels_default() |
| Survey step metadata | collect_mr_label_metadata() |
collect_mr_levels_metadata() |
| Common prefix | collect_mr_label_lcp() |
collect_mr_levels_lcp() |
| Complete labels or names | collect_mr_label_fallback() |
collect_mr_levels_fallback() |
For columns already typed with
g6tr.survey::step_set_mr(), the metadata helpers read the
question and option text recorded by that step. To require this strategy
instead of using the automatic chain:
data <- collect_mr(
data_typed,
news_sources = starts_with("source_"),
label = collect_mr_label_metadata,
levels = collect_mr_levels_metadata
)Metadata and LCP helpers return NULL when their strategy
cannot extract valid values. The defaults then try the next strategy.
The fallback label is NULL unless all components share an
identical label; fallback levels use source names when complete labels
are unavailable or duplicated.