CLiREN-LMS
Data Cleaning and Preparation in R

Common Cleaning Risks and How to Avoid Them

7.10 Common Cleaning Risks and How to Avoid Them

30-45 minutes Applied Step 3 of 9
Reading 1

7.10 Common Cleaning Risks and How to Avoid Them

3 / 9
R makes data manipulation efficient, but efficiency can magnify errors. A single incorrect line of code can affect every record. For that reason, cleaning scripts should be developed cautiously, reviewed, and tested. The goal is not to be afraid of R, but to use it with professional discipline. One common risk is overwriting original variables. If a script changes `sex` from numeric codes to labels in the same column, it may become difficult to compare the transformed values to the raw export. A safer approach during preparation is to create a new variable, such as `sex_label`, while preserving `sex`. Once the workflow is fully defined, the analysis dataset specification can determine which variables are retained. Another risk is silently dropping records. Functions such as `filter()` are essential, but they remove rows. If a script filters to baseline visits and then later assumes the dataset represents all participants, the report may be misleading. When rows are excluded, the script should make the reason explicit and, where appropriate, produce a count. ```r n_before <- nrow(enrollment_prepared) baseline_dataset <- enrollment_prepared |> filter(visit_name == "Baseline") n_after <- nrow(baseline_dataset) tibble( step = "Filter to baseline visit", records_before = n_before, records_after = n_after, records_removed = n_before - n_after ) ``` This simple count helps document what happened. For formal workflows, such counts can be accumulated into a processing log. A third risk is incorrect date handling. Dates imported as text may appear correct but fail comparisons. Different systems may use different date formats. A date such as `03/04/2026` may mean 3 April or 4 March depending on locale. Clinical data management teams should standardize date formats and check imported date types. ```r enrollment_prepared <- enrollment_data |> mutate( consent_date = lubridate::ymd(consent_date), enrollment_date = lubridate::ymd(enrollment_date) ) ``` This code assumes dates are in year-month-day format. If the source uses another format, a different parser is needed. The data manager must verify the source format before conversion. Another risk is confusing missing, zero, and not applicable. A missing laboratory result is not the same as a result of zero. A procedure that was not applicable is not the same as a procedure that should have been done but was missed. The CRF and data dictionary should define how these states are captured.
RiskExamplePrevention
Overwriting raw variablesReplacing numeric code with label in same columnCreate new derived or labeled variable
Silent row lossFiltering without counting exclusionsRecord before and after counts
Incorrect datesTreating text dates as real datesParse dates explicitly and inspect results
Wrong recodingReversing `1 = Female`, `2 = Male`Validate against data dictionary
Uncontrolled missing codesTreating `999` as a real valueDefine and convert special missing codes carefully
Unreviewed automationSending query lists without human reviewRequire data manager review before action
Quality in R is built through small habits: preserve raw inputs, name objects clearly, check dimensions, review summaries, document assumptions, write outputs separately, and compare results with expectations. These habits are not glamorous, but they prevent many serious errors.