Clinical data preparation often involves creating datasets that are easier to analyze or report. This may include selecting relevant variables, joining data across instruments, reshaping repeated measures, deriving endpoint variables, and applying labels. Data managers and statisticians should collaborate closely at this stage. The data manager understands the database structure, CRF logic, query history, and operational context. The statistician understands the analysis plan and statistical requirements. Both perspectives are needed.
Suppose an enrollment dataset contains participant-level variables and a laboratory dataset contains multiple test results per participant. A simple join may add selected baseline laboratory values to the enrollment dataset:
baseline_labs <- lab_data |>
filter(visit_name == “Baseline”) |>
select(participant_id, haemoglobin, creatinine, lab_result_date)
analysis_dataset <- enrollment_prepared |>
left_join(baseline_labs, by = “participant_id”)
This code assumes there is one baseline lab record per participant. If there are multiple baseline lab records, the join may duplicate rows. The data manager should check the structure before joining:
baseline_labs |>
count(participant_id) |>
filter(n > 1)
If duplicates exist, the team must define which result to use. Should the analysis use the earliest result, the result closest to enrollment, the result before treatment, or the result reviewed by the investigator? R can implement any of these rules, but the rule must come from the study plan.
Preparing data for reporting may also involve creating categorical summaries:
analysis_dataset <- analysis_dataset |>
mutate(
age_group = case_when(
age_years_derived < 30 ~ “<30”,
age_years_derived >= 30 & age_years_derived < 45 ~ “30-44”,
age_years_derived >= 45 & age_years_derived < 60 ~ “45-59”,
age_years_derived >= 60 ~ “60+”,
TRUE ~ “Missing”
)
)
Age grouping should be meaningful. Arbitrary categories can obscure interpretation. If the protocol or analysis plan defines age groups, the script should follow those definitions.
The analysis-ready dataset should be accompanied by documentation. At minimum, the team should know:
1. Which raw files were used.
2. Which script generated the dataset.
3. Which date and script version were used.
4. Which variables were included.
5. Which variables were derived.
6. Which records were excluded and why.
7. Which unresolved queries remained at the time of dataset creation.
This documentation protects the credibility of the analysis. It also helps when a manuscript reviewer, sponsor, ethics committee, or regulator asks how the dataset was prepared.