Ch 7 Extension: Cleaning Messy Data

Companion to Chapter 7 — Preparing Data · ~35–45 minutes

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).

A note on AI tools: You may use AI assistants (ChatGPT, Claude, etc.) to help debug your R code — that's a realistic professional skill. You may not use AI to identify the data problems, explain what they mean, or write your reflection answers. If you use AI for any part of this activity, note where at the end.

Setup

No packages needed. Load the data and take a first look:

data <- read.csv("https://jkoxford-a11y.github.io/awesomesaucestats/data/messy_study.csv") str(data) head(data, 10) summary(data)

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.

Step 0: Try the test anyway

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):

t.test(post_score ~ group, data = data)
S1 — record in your answer document: R stops with an error instead of a result. Paste the exact error message.
S2 — record in your answer document: In plain language: why did it fail? (Hint: run table(data$group) and look at how many distinct values there are.)
This is the whole point of the activity: a test can't run on data it can't make sense of, and a wrong-shaped variable fails loudly. A wrong value inside an otherwise fine-looking variable — like the sentinel codes below — fails silently instead, which is worse.

Part A: Find the problems

Before writing any cleaning code, find every data problem you can.

A1 — record in your answer document: Run 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.
A2 — record in your answer document: Using the same function as A1, check group for the same kind of problem. Are all the values consistent? List any problems.
A3 — record in your answer document: Run 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?
A4 — record in your answer document: Run 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?
A5 — record in your answer document: How many rows have a missing 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().

Part B: Clean the data

Fix the problems from Part A, in this order.

B1. Remove the duplicate row

data_clean <- data[!duplicated(data$participant_id), ]

This keeps every row except the repeat and saves the result as data_clean. From here on, work on data_clean.

B2. Fix 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.

data_clean$gender <- ifelse(data_clean$gender == "female", "F", data_clean$gender) table(data_clean$gender) # check: only F and M now

Read it as: if gender is "female", make it "F"; otherwise, keep what's there.

B3. Fix group (your turn)

B3 — record in your answer document: 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.

B4. Recode sentinel values as NA

data_clean$age <- ifelse(_______________, NA, data_clean$age) data_clean$post_score <- ifelse(_______________, NA, data_clean$post_score)

B5. Verify the fix

Make the two grouping variables factors:

data_clean$group <- as.factor(data_clean$group) data_clean$gender <- as.factor(data_clean$gender)
B5 — record in your answer document: Re-run your checks from Part A on 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.

Part C: Run it again

C1 — record in your answer document: Run the Step 0 t-test again, this time on data_clean. Does it run now? At the bottom of the output, R prints the mean for each group. Which group scored higher?
C2 — record in your answer document: The raw file had 31 rows. How many rows does 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?

Part D: Reflect

D1 — record in your answer document: Before you ran any analysis, you had to make several judgment calls: whether to recode age = 99 as 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?
D2 — record in your answer document: You found a duplicate participant ID. Name two scenarios in which a duplicate could arise: one that represents a data entry error, and one that could represent a legitimate design feature.
D3 — record in your answer document: The 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?