CLiREN-LMS
Data Cleaning and Preparation in R

Common Cleaning Risks and How to Avoid Them

Common Cleaning Risks and How to Avoid Them

30-45 minutes Applied Step 5 of 9
Accordion

Common Cleaning Risks and How to Avoid Them

5 / 9
Accordion

Common Cleaning Risks and How to Avoid Them

Part 1
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.
Part 2
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. 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.
Part 3
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. 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.