Skip to contents

Groups variables by their shared question_preface metadata and classifies each group as one of "single", "sata", or "battery". This is the single source of truth used by downstream export functions to decide how to render each question.

Usage

classify_question_type(x, ..., variable = NULL)

Arguments

x

A survey design object or data.frame.

...

<tidy-select> Variables to classify. Supports selection helpers: tidyselect::starts_with(), tidyselect::all_of(), tidyselect::any_of(), etc. Cannot be combined with variable.

variable

character. Alternative programmatic interface: character vector of variable names. Cannot be combined with ....

Value

A tibble with columns:

  • variable (character) — variable name

  • question_preface (character) — the preface, or NA if none

  • type (character) — one of "single", "sata", or "battery"

  • group (integer) — group id; variables with the same non-NA preface share a group

Details

The classification rules, applied per requested variable:

  1. If the variable has no question_preface, or is the only requested variable sharing its preface, type = "single".

  2. If a question_preface is shared by 2+ requested variables and at least one is flagged via set_sata(), all variables in that group get type = "sata".

  3. Otherwise (shared preface, no SATA flag), all variables in the group get type = "battery".

Group numbers are assigned sequentially by first appearance in the input.

Examples

d <- as_survey(nhanes_2017, ids = sdmvpsu, weights = wtint2yr,
               strata = sdmvstra, nest = TRUE)
d <- set_question_preface(d, riagendr = "Demographics",
                             ridageyr = "Demographics")
d <- set_sata(d, riagendr, ridageyr)
classify_question_type(d, riagendr, ridageyr, bpxsy1)
#> # A tibble: 3 × 4
#>   variable question_preface type   group
#>   <chr>    <chr>            <chr>  <int>
#> 1 riagendr Demographics     sata       1
#> 2 ridageyr Demographics     sata       1
#> 3 bpxsy1   NA               single     2