A multiple_response object is a logical matrix stored as one data-frame
column. Rows represent respondents and columns represent answer options.
TRUE means selected, FALSE means not selected, and NA means missing.
An all-FALSE row is a valid response with no selections.
The object behaves as a vector of respondent answers: length(x) counts
respondents and x[i] selects whole respondent rows, preserving all options.
Explicit two-dimensional indexing, such as x[i, j], selects options too.
levels(x) returns the response levels stored in the matrix column names.
vctrs slicing, recycling, and concatenation also operate on respondent rows.
Printing shows the labeled matrix. format() returns a compact string per
respondent, such as "{Television, Radio}" or "{none}". A missing option
appears with a ? prefix, and an entirely missing response returns NA.
Tibbles use their native matrix-column display for this class.
Usage
multiple_response(x, label = NULL, levels = colnames(x))Arguments
- x
A logical matrix with at least one column. Column names supply the response levels when
levelsis omitted.- label
Optional question label, supplied as a single string.
- levels
Unique, nonempty response levels in matrix-column order. These become the matrix column names. Defaults to the existing column names.
Value
A logical matrix of class multiple_response, with question and
option labels. Row subsetting preserves the class. Explicit option
subsetting preserves it when the result is a nonempty-column matrix.
Examples
answers <- multiple_response(
cbind(tv = c(TRUE, FALSE), radio = c(FALSE, TRUE)),
label = "News sources",
levels = c("Television", "Radio")
)
answers
#> <multiple_response> News sources
#> 2 respondents x 2 options
#> Television Radio
#> [1,] TRUE FALSE
#> [2,] FALSE TRUE
tibble::tibble(id = 1:2, sources = answers)
#> # A tibble: 2 × 2
#> id sources[,"Television"] [,"Radio"]
#> <int> <lgl> <lgl>
#> 1 1 TRUE FALSE
#> 2 2 FALSE TRUE