rd_factor: Convert Variables to Factors in a REDCap Dataset

View source: R/rd_factor.R

rd_factorR Documentation

Convert Variables to Factors in a REDCap Dataset

Description

[Stable]

Converts variables in a REDCap dataset with associated .factor columns into actual factor variables, while allowing the exclusion of specific variables. Ensures consistency with the dataset and preserves variable labels.

Usage

rd_factor(
  project = NULL,
  data = NULL,
  dic = NULL,
  event_form = NULL,
  exclude = NULL
)

Arguments

project

A list containing the REDCap data, dictionary, and event mapping (expected redcap_data() output). Overrides data, dic, and event_form.

data

A data.frame or tibble with the REDCap dataset.

dic

A data.frame with the REDCap dictionary.

event_form

Only applicable for longitudinal projects (presence of events). Event-to-form mapping for longitudinal projects.

exclude

Optional character vector of variable names (use original names without the .factor suffix) to exclude from conversion.

Details

The function looks for columns ending in .factor and replaces the original variable values with those .factor values (converted to factors). The variables in exclude, the system columns below and the variables whose labels are of several codes (with a warning) keep both columns. A .factor column that isn't a factor (e.g. text, after modifying the data) is converted to a factor, with the levels in the order of the options of the dictionary. It preserves variable labels. The exclude argument must contain base variable names (no .factor suffix); if any .factor names are passed to exclude the function will throw an informative error. The columns redcap_event_name, redcap_repeat_instrument and redcap_data_access_group (and their .factor counterparts) are handled specially to avoid altering event or access-group data. A .factor column whose original variable no longer exists (e.g. removed with rd_delete_vars()), also of these columns, is removed with a warning.

Use rd_factor() before rd_dictionary(): the branching logic and calculations that rd_dictionary() translates to R compare the variables with their original codes, so they stop working once the variables are factors. If the dictionary already has logic translated to R that uses any of the converted variables, a warning is shown.

Value

A list with the following elements:

data

The transformed dataset with .factor columns applied as factors.

dictionary

The dictionary used (unchanged).

event_form

The event-form mapping used (if applicable).

results

A brief text summary of the transformation.

See Also

Other process functions: rd_checkbox(), rd_dates(), rd_delete_vars(), rd_dictionary(), rd_insert_na(), rd_recalculate(), rd_rlogic(), rd_split(), rd_transform()

Examples

## Not run: 
result <- rd_factor(covican)
result <- rd_factor(covican, exclude = c("available_analytics", "urine_culture"))
transformed_data <- result$data

## End(Not run)


REDCapDM documentation built on Nov. 27, 2026, 9:06 a.m.