This function is used to process checkbox variables in a REDCap dataset. By default, it changes their default categories ("Unchecked" and "Checked") to new ones ("No" and "Yes). Optionally, the function can also evaluate the branching logic for checkbox fields and adjust the data and dictionary accordingly.
Usage
rd_checkbox(
project = NULL,
data = NULL,
dic = NULL,
event_form = NULL,
checkbox_labels = c("No", "Yes"),
checkbox_names = TRUE,
na_logic = "missing"
)Arguments
- project
A list containing the REDCap data, dictionary, and event mapping (expected
redcap_data()output). Overridesdata,dic, andevent_form.- data
A
data.frameortibblewith the REDCap dataset.- dic
A
data.framewith the REDCap dictionary.- event_form
Only applicable for longitudinal projects (presence of events). Event-to-form mapping for longitudinal projects.
- checkbox_labels
Character vector of length 2 for labels of unchecked/checked values. Default:
c("No", "Yes").- checkbox_names
Logical. If
TRUE(default), checkbox columns are renamed using choice labels. WithFALSE, the columns keep their names and each option is a field of the dictionary: running the function again on its result stops with an error instead of converting them again. Without the.factorversions of the options, the values of the columns become the labels; with them (as in the data ofredcap_data()), the columns keep their codes 0/1 and their.factorversions take the labels.- na_logic
Controls how missing values are set based on branching logic. Must be one of
"none"(do nothing),"missing"(set toNAonly when the logic evaluation isNA), or"eval"(set toNAwhen the logic evaluates toFALSEorNA). Defaults to"missing".
Value
A list with:
- data
Transformed dataset with the checkbox options optionally renamed. When the data has the
.factorversions of the options, the original columns keep their codes 0/1 and the.factorversions take the labels ofcheckbox_labels; otherwise the original columns are replaced by factors with these labels.- dictionary
Updated dictionary with checkbox fields expanded and optionally renamed.
- event_form
The
event_formpassed in (if applicable).- results
Summary of transformations and any fields needing review.
Details
Checkbox columns are expected in REDCap wide format (
field___code).Branching logic evaluation requires
event_formfor longitudinal projects.Names are cleaned and truncated to 60 characters; uniqueness is enforced.
Fields that cannot be evaluated are listed in
results.In the branching logic and calculations, the options get their new names and keep their codes
0and1, as in REDCap logic (e.g.[cb(1)] = '1'is[cb_option] = '1', and[cb(1)] + [cb(2)] >= 2is[cb_option] + [cb_other] >= 2).rd_dictionary()writes the labels when it translates the logic into R.
See also
Other process functions:
rd_dates(),
rd_delete_vars(),
rd_dictionary(),
rd_factor(),
rd_insert_na(),
rd_recalculate(),
rd_rlogic(),
rd_split(),
rd_transform()
Examples
# Basic usage with a project object
res <- rd_checkbox(covican)
# With custom labels
res <- rd_checkbox(data = covican$data,
dic = covican$dictionary,
event_form = covican$event_form,
checkbox_labels = c("Not present", "Present"))
# Keep original checkbox names
res <- rd_checkbox(covican, checkbox_names = FALSE)
# Longitudinal project with NA logic
res <- rd_checkbox(data = covican$data,
dic = covican$dictionary,
event_form = covican$event_form,
na_logic = "eval")
