View source: R/format_checkbox.R
format_checkbox | R Documentation |
Modify a data frame representing a 'select all that apply' style survey question. Makes implicit negative responses explicit while keeping non-responses as is.
format_checkbox(df, relabel = TRUE)
df |
A data frame |
relabel |
Should the value label be set as the variable label? |
The steps taken are:
Set the variable label as the (presumably singular) value label if relabel = TRUE
Find the observations that are missing across all columns
Remove value labels, replace missing values with zeroes
Replace values with NA for completely missing observations
A data frame
Sven Halvorson (svenpubmail@gmail.com)
set.seed(0)
checkbox_example = tibble(
TREATMENT = rep(c('treat', 'control'), each = 20),
Q1 = sample(c(1, NA), size = 40, replace = TRUE),
Q2 = sample(c(1, NA), size = 40, replace = TRUE),
Q3 = sample(c(1, NA), size = 40, replace = TRUE)
)
# Note first & third rows are all missing
# Set some value labels:
for(i in 1:3){
lab = 1
names(lab) = LETTERS[i]
expss::val_lab(checkbox_example[paste0('Q',i)]) = lab
}
# Make a new dataframe of just the formatted checkbox questions:
formatted_checkbox = checkbox_example %>%
select(all_of(paste0('Q', 1:3))) %>%
format_checkbox()
attributes(formatted_checkbox[['Q1']])
# Also can do assignment for a chunk of the data frame but it's a little trickier
checkbox_example[,paste0('Q', 1:3)] = format_checkbox(checkbox_example[,paste0('Q', 1:3)])
checkbox_example
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.