Real data almost never arrives in analysis-ready form. Before you can run a single hypothesis test, you typically spend more time cleaning the data than analyzing it. This activity gives you a realistic messy dataset and asks you to find and fix every problem before running an analysis.
The dataset is messy_study.csv — a simulated pre/post study comparing a treatment group
to a control group on a cognitive performance score (0–100).
No packages needed. Load the data and take a first look:
str() shows you the type of every column. head() shows the first rows so you can
eyeball what a single row looks like. summary() gives a quick min/max/mean per numeric column.
The study compares post_score between the two groups. Before doing any cleaning, run the
t-test you'd normally jump straight to (it's the same shape as Error 4 in Chapter 7):
table(data$group) and look at how many
distinct values there are.)
Before writing any cleaning code, find every data problem you can.
table(data$gender). table() counts how many rows fall into each distinct
value of a column. How many distinct values does gender have? How many should there be? List
the problematic values.
group for the same kind of problem. Are all the values
consistent? List any problems.
sum(duplicated(data$participant_id)). It counts how many rows repeat a
participant_id that appeared earlier in the file. How many duplicates are there?
summary() on one column at a time (summary(data$age), then
summary(data$post_score)). Do any values look suspicious? What would you do with a
participant coded as age 99? What about a post_score of 999?
pre_score? Write your own line of code using the same shape as
A3 — sum() wrapped around a check, this time is.na() instead of
duplicated().
Fix the problems from Part A, in this order.
This keeps every row except the repeat and saves the result as data_clean. From here on,
work on data_clean.
gender (worked for you)This is the same ifelse() you used to turn 999 into NA in Chapter 7. The only
difference: text values go in quotation marks.
Read it as: if gender is "female", make it "F"; otherwise, keep
what's there.
group (your turn)group has two bad values: "Treatment" (capital T) and "treatmnt"
(typo). Both should be "treatment". Write two lines in the same shape as B2, one per bad
value, then run table(data_clean$group) to check you have exactly two groups.
NANA (likely a missing-data sentinel code)post_score = 999: recode as NA (same sentinel)Make the two grouping variables factors:
data_clean (same code, different data frame) and confirm:
no duplicate IDs; gender and group have exactly 2 values each; no 99 in
age or 999 in post_score. Then run str(data_clean) and confirm
group and gender are factors.
data_clean. Does it run now? At the bottom
of the output, R prints the mean for each group. Which group scored higher?
data_clean have, and how many of those have a
missing post_score? What does this tell you about the cost of data quality problems?
NA, whether to exclude or retain participants with missing post_score, whether
to fix typos or flag them. In a real study, when should these decisions be made — before or after
seeing the data? Why does it matter?
notes column contained comments like "likely data entry error" and "second 999 value."
Who might have written these notes, and why is documenting data anomalies in the dataset itself a good
practice?