rd_split: Split a REDCap dataset by form or event

View source: R/rd_split.R

rd_splitR Documentation

Split a REDCap dataset by form or event

Description

[Stable]

Splits a REDCap dataset into separate datasets by form or event using the data dictionary. Supports both longitudinal and non-longitudinal projects and can return wide or long formats for repeated measures.

Usage

rd_split(
  project = NULL,
  data = NULL,
  dic = NULL,
  event_form = NULL,
  which = NULL,
  by = "form",
  wide = FALSE
)

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.

which

Optional. A single form or event to extract. If not provided, all forms or events are returned.

by

Character. Criteria to split the dataset: "form" (default) or "event".

wide

Logical. If TRUE (for form-based splits), repeated instances are returned in wide format. Defaults to FALSE.

Details

  • Handles checkbox variables and REDCap default variables (⁠_complete⁠, ⁠_timestamp⁠) appropriately.

  • Columns of the data that aren't fields of the dictionary, nor the .factor or ⁠_mdc⁠ versions of its fields or the options of its checkbox fields (or the .factor version of the ⁠_complete⁠ field of a form), stop the function with an error that names them.

  • For form-based splits in longitudinal projects, uses event_form to map variables to events.

  • Wide format expands repeated instances into multiple columns per record. A form with only the record identifier gives one row per record.

  • Filtering by which allows extracting a single form or event.

  • Projects with repeated instruments are handled by filtering on the redcap_repeat_instrument variable (its codes). The column shows the label of each instrument (its factor version, from redcap_data()), or its code when the label is missing.

Value

Depending on which and wide:

data

Without which, a tibble with one row per form (columns form, events in longitudinal projects, and df) or per event (columns events and df): the dataset of each form or event is in the list-column df (e.g. result$data$df[[1]], or result$data$df[result$data$form == "demographics"][[1]]). With wide = TRUE, the tibble of the forms also has the column max_repeated_instance, the largest number of rows of a record in each form (its events and repetitions): the wide columns are numbered by the position of each row of the record, not by its instance (e.g. the instances 2 and 5 of a record are the columns ⁠_1⁠ and ⁠_2⁠, and the maximum is 2). With which, the data.frame of that form or event.

dictionary

The original REDCap dictionary.

event_form

The original event-form mapping (if applicable).

results

A summary message of the splitting operation.

See Also

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

Examples

# Split by form and return wide format
result <- covican |>
  rd_split(by = "form", wide = TRUE)

print(result)

# Split by event (long format)
result <- covican |>
  rd_split(by = "event")

print(result)


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