Day 11 — Data quality and evidence
A labmate exports the seedling measurements from the lab spreadsheet and sends you the file. You calculate the group means and email them to your supervisor. A week later someone notices that one plant appears twice and one length was typed in centimetres. R never complained: it calculates a precise-looking answer from whatever table it is given. How do you know the table is fit to calculate from?
Today you will:
Week 3 is about describing data. The checked table and these habits carry into Days 12–15 and into the comparisons of week 4.
day11-cleaning.R and write a new derived file.> raw <- read_csv("data/seedlings-messy.csv", show_col_types = FALSE,
+ col_types = cols(.default = col_character()),
+ trim_ws = FALSE)
> dim(raw)
[1] 25 4
> unique(raw$treatment)
[1] "control" "Control" "treated" "treated "
> raw |> count(plant_id) |> filter(n > 1)
# A tibble: 1 × 2
plant_id n
<chr> <int>
1 P01 2
dim(): 25 rows, 4 columns, but the study has 24 plants.unique() lists each different value once: four spellings of two treatments. Reading every column as text (col_character()) and keeping spaces (trim_ws = FALSE) makes "treated " visible.count() counts the rows for each plant ID; filter(n > 1) keeps IDs that occur more than once: P01.> filter(raw, plant_id == "P01")
# A tibble: 2 × 4
plant_id treatment initial_mm final_mm
<chr> <chr> <chr> <chr>
1 P01 control 25.7 44.3
2 P01 control 25.7 44.3
> filter(raw, plant_id %in% c("P06", "P18", "P21"))
# A tibble: 3 × 4
plant_id treatment initial_mm final_mm
<chr> <chr> <chr> <chr>
1 P06 control 24.7 <NA>
2 P18 treated 18.7 mm 39.7
3 P21 treated 23.6 4.38
<NA> = not available). The reading was never recorded.18.7 mm.%in% keeps the rows whose ID is in the list."18.7 mm" becomes the number 18.7 only after we confirm the unit is millimetres.NA (not available).> cleaned <- raw |>
+ mutate(treatment = tolower(trimws(treatment)),
+ initial_mm = if_else(plant_id == "P18", "18.7", initial_mm),
+ final_mm = if_else(plant_id == "P21", "43.8", final_mm)) |>
+ distinct() |>
+ mutate(initial_mm = parse_double(initial_mm),
+ final_mm = parse_double(final_mm),
+ growth_mm = final_mm - initial_mm)
> filter(cleaned, plant_id %in% c("P06", "P18", "P21"))
# A tibble: 3 × 5
plant_id treatment initial_mm final_mm growth_mm
<chr> <chr> <dbl> <dbl> <dbl>
1 P06 control 24.7 NA NA
2 P18 treated 18.7 39.7 21
3 P21 treated 23.6 43.8 20.2
trimws() removes edge spaces and tolower() turns Control into control. if_else() changes only the named plant.distinct() drops the identical second P01 row. parse_double() turns the text into numbers (<dbl>); the blank becomes NA.> nrow(cleaned)
[1] 24
> sum(is.na(cleaned$growth_mm))
[1] 1
> mean(cleaned$growth_mm)
[1] NA
> mean(cleaned$growth_mm, na.rm = TRUE)
[1] 18.48696
is.na() marks unknown values; sum() counts them: 1.mean() gives NA: one value is unknown, so R will not guess.na.rm = TRUE (“remove NA”) averages the 23 available growths. Report it as “n = 23 of 24 plants”.> write_csv(cleaned, "output/seedlings-cleaned.csv")
> list.files("output")
[1] "seedlings-cleaned.csv"
> check_copy <- read_csv("output/seedlings-cleaned.csv",
+ show_col_types = FALSE)
> nrow(check_copy)
[1] 24
> anyDuplicated(check_copy$plant_id)
[1] 0
> sum(is.na(check_copy$growth_mm))
[1] 1
write_csv() prints nothing. list.files() shows the new file in output/.anyDuplicated() gives 0), one unknown growth.A second class sends data/seedlings-transfer.csv: 18 plants, 9 per group.
Before importing, write down what you expect: what one row is, which ID must be unique, column names, units, allowed labels. Then check and record what you observe.
| Concept | Key fact |
|---|---|
| Inspect first | Read as text (col_character(), trim_ws = FALSE); unique(), count() reveal labels and repeated IDs |
| Evidence | A value is changed only when a source record supports it, never because it looks odd |
| Raw file | Never edited; repairs live in a script that writes a separate derived file |
| Duplicates | Compare all fields and the design; distinct() only for confirmed identical rows |
| Missing | Unknown stays NA; a zero is a claim (P06 would “grow” −24.7 mm) |
| Counts | 24 plant records, 23 usable growths; report the n behind every mean |
| Check | Read the saved file back; restart R and rerun the script |
Next (Day 12, variation): with a table we can trust, we describe how much individual plants differ from each other, not only their average.
Now: open the Day 11 exercises. Leave with day11-cleaning.R, output/seedlings-cleaned.csv and your cleaning notes.