Day 19 of 30·Clinical Data Handling & CDISC Basics
Data Quality Checks & Validation in R
Programmatic checks, duplicate detection and reconciliation.
Learning objectives
- 1Write data checks that return offending records.
- 2Detect duplicate keys and out-of-range values.
- 3Use assertions to gate a pipeline.
- 4Produce a simple discrepancy report.
Key SAS® → R comparison
| Task | SAS® | R |
|---|---|---|
| Find dup keys | proc sort nodupkey + log | count(key) |> filter(n>1) |
| Range check | if x<lo or x>hi then ... | filter(x<lo | x>hi) |
| Assert | manual log scan | assertr / stopifnot |
Code examples
SAS®
proc sort data=lb nodupkey dupout=dups;
by usubjid lbtestcd visitnum;
run;R
dups <- lb |>
count(usubjid, lbtestcd, visitnum) |>
filter(n > 1)Note: Returns exactly which key combinations repeat — a ready-made discrepancy listing.
Explanation
R makes data-quality checks first-class: instead of scanning a log, you write expressions that return the offending rows as a dataset you can review, count, or export. Duplicate-key detection, range checks and cross-dataset reconciliation all become small, testable pipelines.
Assertion packages such as assertr let you embed checks directly in a pipeline so it halts when an expectation is violated, giving you the audit trail validation reviewers expect.
Practice exercise
Write checks that return duplicate LB keys, AVAL values outside a plausible range, and subjects present in ADAE but missing from ADSL; export each as a discrepancy listing.