Tabulate a range of check-all-that-apply response columns in a single table.
Source:R/survey_utils.R
check_all_count.Rd
This function is to be run on columns treated with check_all_recode()
.
Takes a data.frame and range of columns containing all answer choices to a check-all-that-apply question and tabulates the results. People who did not select any choices (i.e., they did not answer the question) are omitted from the denominator. For this to make sense, the question's choices should be MECE, or there should be an NA option.
This works with an "Other" open-response text field, which will be recoded to a binary variable with check_all_recode
.
Arguments
- dat
a data.frame with survey data
- ...
unquoted column names containing the range of the answer choices. Can be specified individually, as a range, i.e.,
q1_1:q1_5
, or using other helper functions fromdplyr::select()
.
Examples
x <- data.frame( # 4th person didn't respond at all
unrelated = 1:5,
q1_1 = c("a", "a", "a", NA, NA),
q1_2 = c("b", "b", NA, NA, NA),
q1_3 = c(NA, NA, "c", NA, NA),
q1_other = c(NA, "something else", NA, NA, "not any of these")
)
library(dplyr) # for the %>% pipe
#>
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#>
#> filter, lag
#> The following objects are masked from ‘package:base’:
#>
#> intersect, setdiff, setequal, union
x %>%
check_all_recode(q1_1:q1_other) %>%
check_all_count(q1_1:q1_other)
#> Warning: column 4 has multiple values besides NA; not sure which is the question text. Guessing this an "Other (please specify)" column.
#> response n percent
#> a 3 0.75
#> b 2 0.50
#> c 1 0.25
#> Other 2 0.50
# You can use any of the dplyr::select() helpers to identify the columns:
x %>%
check_all_recode(contains("q1")) %>%
check_all_count(contains("q1"))
#> Warning: column 4 has multiple values besides NA; not sure which is the question text. Guessing this an "Other (please specify)" column.
#> response n percent
#> a 3 0.75
#> b 2 0.50
#> c 1 0.25
#> Other 2 0.50