Guides  /  data cleaning

Data cleaning: a practical guide

Data cleaning routinely takes longer than the analysis it prepares for, and the decisions taken during it affect results more than the choice of test. This guide sets out a defensible workflow, explains the missing-data distinctions that determine what you are allowed to do, and covers how to document the process so it can be reproduced and defended.

Hafiz Ahmad Tariq Written and reviewed by Hafiz Ahmad Tariq, Senior Biostatistician
Updated 17 August 202618 min read
What is data cleaning?

Data cleaning is the process of preparing a raw dataset for analysis: correcting structural problems, checking values are valid and within range, identifying duplicates, deciding how to handle missing values and outliers, and constructing derived variables. Every decision should be recorded and applied through a script, so the cleaned dataset can be reproduced exactly from the raw file.

Definition

Why cleaning decisions matter

Cleaning decisions change results more often than the choice of statistical test does. Whether an outlier stays, how missing values are handled, and where a variable is cut all move the answer — frequently by more than switching between a t-test and a Mann-Whitney would.

That makes them analytic decisions, not clerical ones, and they carry the same obligations: decide the rule before seeing whether it helps your hypothesis, record it, and report it. A cleaning stage that is never described in the methods section is a set of undocumented choices sitting between the data and every number you report.

The scale of the problem is easy to underestimate. Studies giving the same raw dataset to many independent analysts have repeatedly found substantial variation in results — not because anyone made an error, but because each team made a different defensible chain of decisions about exclusions, missingness and variable construction. Some of those studies found teams reaching opposite conclusions from identical data. That is the strongest available argument for documenting every choice: not that any one of them is wrong, but that a reader cannot evaluate your result without knowing which path you took.

Set the rules before you look at the outcome

Deciding to exclude an outlier after noticing that removing it makes a result significant is a recognised form of p-hacking, and it is invisible to a reader unless you disclose it. Write the exclusion rules into your analysis plan, and if you deviate, say so and report the result both ways.

Overview

The workflow

RAW never edited structureone row per unit validityranges, types missingnesspattern first outliersinvestigate ready analyse EVERY step recorded in a script, and every decision logged with its reason Cleaning by hand in a spreadsheet is unreproducible: nobody, including you, can later say what was changed or why. A script re-runs from the raw file and produces the identical cleaned dataset every time. This is the single highest-value habit in applied data work, and it costs almost nothing to adopt.
Cleaning runs from an untouched raw file to an analysis-ready dataset, with every step scripted and every decision logged.
Never edit the raw file — keep it read-only and work from a copy
Script every step so the cleaned dataset regenerates from raw in one run
Log every decision with its reason and the number of cases affected
Check after each step that row counts and totals are what you expect
Keep the cleaning separate from the analysis script

Version the script alongside the manuscript, so the exact code that produced a given set of numbers can always be recovered later.

Why scripting matters more than the tool

R, Python, Stata and SPSS syntax all work. Point-and-click cleaning in a spreadsheet does not, because six months later neither you nor an examiner can establish what was changed. If a viva question asks how many cases were excluded and why, a script answers it in seconds and a manually edited spreadsheet cannot answer it at all.

Structure

Step 1: structure

Before checking any values, confirm the dataset is shaped correctly.

One row per unit of analysis — and be clear what your unit is
One variable per column, with no columns holding two pieces of information
Consistent variable names — no spaces, no leading digits, readable
Correct data types — numbers stored as numbers, dates as dates
Value labels documented in a codebook

Being clear about the unit of analysis sounds trivial and frequently is not. A dataset of clinical appointments might have one row per appointment, but if your question is about patients, the unit is the patient and appointments must be aggregated first — otherwise a patient attending twelve times contributes twelve observations and dominates the analysis. Getting this wrong is easy to do and produces conclusions about a population you did not intend to study.

Wide or long?

FormatOne row perNeeded for
WideParticipant, with repeated measures in columnsRepeated measures ANOVA, correlations between time points
LongObservation, with a time or condition columnMixed models, multilevel models, most plotting

Reshaping between the two is straightforward in any package, but knowing which your intended analysis requires prevents a great deal of wasted effort. Mixed models need long format; classic repeated measures ANOVA needs wide.

Watch out for dates and identifiers

Spreadsheet software silently converts some values — identifiers with leading zeros lose them, and certain codes are read as dates. Check any ID column has retained its original form, and import as text where necessary. This class of error is invisible until it breaks a merge.

Validity

Step 2: validity checks

Every variable should be checked against what it can legitimately contain.

CheckLooking forExample
RangeValues outside the possibleAge of 999; a 5-point item scored 7
TypeText in a numeric field“approx 40” in an age column
CategoriesUnexpected or inconsistent levels“Female”, “female”, “F” as three categories
Logical consistencyImpossible combinationsAge 19 with 25 years of experience
DatesOrdering and plausibilityA follow-up date before baseline

Run a frequency table for every categorical variable and minimum, maximum and a histogram for every continuous one. This takes minutes and catches most problems. Inconsistent category labels are especially common with free-text entry and will silently split a group in two if not caught.

Missing value codes are a classic trap

Surveys often code missing as 99, 999 or −1. If these are not declared as missing before analysis, a mean age will be computed including the 999s and will be badly wrong — without any error message. Check the codebook and declare them explicitly.

Duplicates

Step 3: duplicates

Duplicates arise from double submission, repeated data entry and merges gone wrong. They inflate the sample and violate the independence assumption every test relies on.

Exact duplicates — identical across all fields; almost always safe to remove
Duplicate identifiers with different data — investigate individually, do not delete blindly
Near-duplicates — same person, slightly different entry; check timestamps and identifying fields
Legitimate repeats — in long format the same ID appears once per occasion, which is correct

Online surveys need a slightly different eye. Repeat submissions from one respondent may share an IP address or a device fingerprint but differ in every answer, and platforms sometimes record a partial submission alongside the completed one. Decide in advance which record you will keep — usually the most complete, or the first — and apply that rule consistently rather than choosing case by case.

Duplicates after a merge mean the join was wrong

If a merge multiplies your row count, the key was not unique in one of the tables. Fix the join rather than deleting the surplus rows, because deleting them silently discards whichever record happened to come second.

Missingness

Step 4: missing data

Handling missing data correctly requires knowing why it is missing, and that is a judgement rather than a test result.

MCAR missing completely at random unrelated to anything e.g. a page was lost deletion is safe, just wasteful MAR missing at random explained by OTHER variables you measured multiple imputation works MNAR missing not at random related to the missing value itself no fix; a limitation to report You cannot test which you have. MCAR is testable against MAR; MAR versus MNAR is a judgement from what you know. Highest earners declining to state income is MNAR — and imputation cannot recover what was never observed.
The three mechanisms. Only the first can be tested for directly, and only the first two can be handled well.
ApproachHow it worksWhen acceptable
Listwise deletionDrop any case with a missing valueMCAR, and little missingness
Pairwise deletionUse all available data per analysisRarely — different n per statistic causes problems
Mean imputationReplace with the variable meanAlmost never — shrinks variance, distorts correlations
Prorated scale scoreMean of answered items, scaled upIf a stated minimum of items was answered
Multiple imputationGenerate several plausible datasets and poolMAR; the preferred approach
Maximum likelihoodEstimate using all available dataMAR; built into many model packages

Why mean imputation is worse than it looks

Replacing missing values with the mean adds cases that sit exactly at the centre, which artificially reduces the variance and pulls every correlation towards zero. It also produces standard errors that are too small, because the analysis treats imputed values as though they had been observed. It is simple, widely used, and one of the few genuinely indefensible choices in this list.

A practical distinction worth drawing is between item-level and unit-level missingness. Item-level means a respondent skipped some questions, and prorating or imputing at the item level is usually reasonable. Unit-level means a participant is absent entirely from a wave or dropped out, which is a different problem and, in a trial, one that bears directly on whether randomisation still holds. They are often reported together as a single percentage, which obscures the distinction that matters.

Report the missingness before handling it

Give the percentage missing per variable, describe the pattern, and state which mechanism you judged it to be and on what grounds. Above about 5% missing on a key variable, listwise deletion needs an explicit justification; above 20%, most reviewers will expect multiple imputation or a serious discussion of why it was not used.

Outliers

Step 5: outliers

An outlier is an unusual value, not automatically a wrong one. The first question is always why it is there, and the answer determines what to do.

CauseWhat to do
Data entry errorCorrect from source if possible; otherwise set to missing
Impossible valueSet to missing and record it
Participant outside the target populationExclude, using a pre-specified rule
Genuine extreme valueKeep it — it is real data

Detection

MethodRuleNote
BoxplotBeyond 1.5 × IQR from the quartilesStandard visual check
z-score|z| > 3.29Assumes normality; the mean is itself pulled by outliers
Mahalanobis distanceChi-square cut-offFor multivariate outliers
Cook's distanceInfluence on a fitted modelIdentifies cases that change the result

Cook's distance asks the most useful question. A value can be extreme without affecting anything, and a value can be unremarkable in isolation while exerting substantial leverage on a regression line. What matters is influence on the conclusion, not distance from the mean.

Report the analysis both ways

Where a genuine extreme value is retained but might be questioned, run the analysis with and without it and report both. If the conclusion holds, that strengthens it considerably; if it does not, that is an important finding about the fragility of the result rather than something to conceal.

Multivariate outliers deserve a mention because they are invisible to any single-variable check. A participant aged 19 is unremarkable, and 25 years of professional experience is unremarkable, but the combination is impossible. Neither value appears in a boxplot of its own variable. Mahalanobis distance flags exactly this kind of case, and running it before a multivariate analysis catches problems that univariate screening cannot.

Alternatives to deletion

Winsorising — replace extremes with the nearest acceptable value; keeps the case, reduces influence
Transformation — a log transform compresses a long right tail
Robust methods — trimmed means or robust regression, which downweight rather than exclude
Rank-based tests — ranking removes the influence of extreme values entirely

Derived variables

Step 6: derived variables

Most datasets need variables constructing before analysis, and each construction is a decision to record.

Reverse-score negatively worded items before computing any total — on a 5-point scale, replace x with 6 − x
Compute scale totals only after establishing reliability
Set the missing-item rule for scale scores in advance
Recode categories with a documented mapping
Centre continuous predictors where the intercept needs to be interpretable

Composite variables built by combining several sources need particular care about their direction and scale. If one contributing measure runs from 1 to 5 and another from 0 to 100, summing them lets the second dominate entirely; standardise first if each is meant to contribute equally. Record the formula used, because a composite defined slightly differently in two chapters of the same thesis is a common and avoidable inconsistency.

Check reverse scoring before you trust a total

Run a correlation matrix of the items. If one correlates negatively with all the others, it is reverse-worded and has not been reverse-scored. Failing to catch this corrupts every total and every reliability coefficient that follows, and a negative Cronbach's alpha is almost always this error rather than a real finding.

Categorising continuous variables

Cutting a continuous variable into groups — median splits, tertiles, high and low — is common and usually costs you. It discards real information, typically sacrifices around 20% of statistical power, and treats two people either side of an arbitrary cut as different while treating the extremes of each group as identical. Keep the variable continuous unless the categories are substantively meaningful, such as a clinically established threshold.

Documentation

Documenting it reproducibly

ArtefactContents
Raw dataUntouched, read-only, backed up
Cleaning scriptEvery step, commented, running raw → clean
Cleaning logEach decision, its reason, and cases affected
CodebookEvery variable: type, range, labels, missing codes
Cleaned datasetRegenerable at any time from the script

In the methods section, report the number of cases at each stage — recruited, completed, excluded and why, analysed. This is the equivalent of a PRISMA flow for participants, and reviewers check that the numbers reconcile.

A sentence that earns marks

“Of 248 responses, 12 were removed as incomplete (under 80% of items answered), 4 as duplicate submissions identified by matching timestamp and IP, and 2 for completion times under 90 seconds against a median of 11 minutes, leaving 230 for analysis. Exclusion criteria were specified before data inspection. Missing item-level data (1.8%) were handled by prorating scale scores where at least 80% of items were answered.”

Have your dataset cleaned and documented properly

Send the raw file and your codebook. A named statistician structures and validates the dataset, handles missingness appropriately, and returns a commented script alongside a cleaning log you can cite in your methods.

See data cleaning

Pitfalls

Six mistakes that cost marks

1. Cleaning by hand in a spreadsheet

Unreproducible, and impossible to defend when asked what was changed.

2. Deciding exclusion rules after seeing the results

Set them in advance. If you deviate, disclose it and report both versions.

3. Mean imputation

It shrinks variance, distorts correlations and produces standard errors that are too small.

4. Not declaring missing value codes

A 999 treated as a real value corrupts every statistic silently, with no error message.

5. Deleting outliers because they are inconvenient

Establish why the value is there. A genuine extreme value is data, not noise.

6. Forgetting to reverse-score

It corrupts every scale total and reliability estimate computed afterwards.

Answers

Frequently asked questions

What is data cleaning in research?

The process of preparing raw data for analysis: fixing structure, checking values are valid, removing duplicates, deciding how to treat missing values and outliers, and constructing derived variables. Every decision should be scripted and logged, because these choices affect results as much as the choice of statistical test.

Should I delete outliers?

Only if you can establish why the value is there. Data entry errors and impossible values should be corrected or set to missing; participants outside your target population can be excluded under a pre-specified rule. A genuine extreme value is real data and should normally be kept, with the analysis reported both with and without it.

What is the difference between MCAR, MAR and MNAR?

MCAR means missingness is unrelated to anything, so deletion is safe though wasteful. MAR means it is explained by other variables you measured, which is when multiple imputation works well. MNAR means it depends on the missing value itself — highest earners declining to state income — and no technique recovers it.

Is mean imputation acceptable?

Almost never. Replacing missing values with the mean adds cases at the centre of the distribution, which artificially reduces variance, pulls correlations towards zero and produces standard errors that are too small. Multiple imputation or maximum likelihood estimation are the defensible alternatives.

How much missing data is too much?

There is no fixed threshold. Below about 5% on a key variable, listwise deletion is usually tolerated; above 20%, most reviewers will expect multiple imputation or a substantial discussion. What matters more than the amount is the mechanism — a small amount of MNAR data can bias results more than a large amount of MCAR.

Should I use a median split to create groups?

Generally no. Categorising a continuous variable discards information, typically costs around 20% of statistical power, and treats participants either side of an arbitrary cut-off as different while treating the extremes within each group as the same. Keep it continuous unless the categories are substantively meaningful.

How do I document my data cleaning?

Keep the raw file untouched, do all cleaning in a commented script that runs from raw to cleaned, maintain a log of each decision with its reason and the cases affected, and keep a codebook. In the methods section, report case numbers at each stage so a reader can reconcile them.

How long does data cleaning take?

Frequently longer than the analysis. For a moderately sized survey dataset, several days is normal, and a messy multi-source dataset can take weeks. Budget for it explicitly, since underestimating it is one of the more common causes of a project running late.

Send the data. Get a fixed quote.

Attach your dataset or just describe the project. A named statistician replies with a price and a deadline, usually within one working day.