Instructor notes: Session 4

Data Structures — Vectors, Lists, Subsetting, and Reshaping

→ Open the Session 4 lesson

Timing

Part Topic Time (incl. TRY IT)
1 Set up 5 min
2 Vectors and types 12 min
3 Vectorization 14 min
4 Subsetting with base R 20 min
5 Lists 15 min
6 Tidy data and pivot_longer() 18 min
7 pivot_wider() 12 min
Wrap-up and preview 5 min
Total 101 min

Short on time? Do TRY IT 7 (district × vaccination table) together as a live demo instead of a solo exercise, and leave the ⭐ Bonus as homework. Don’t cut TRY IT 4 — the base-R NA trap is the key lesson of the session.

Before you start

  • Learners need processed_data/combined_linelist.rds and processed_data/measles_final.rds from Session 3. Ready-made copies are in processed_data/ for anyone who missed it.
  • Learners type into student_scripts/Session 4 - Data Structures_STUDENT.R; the filled-in version is solutions/Session 4 - Data Structures_SOLUTIONS.R.
  • Remind everyone of the sticky notes: green = done with the TRY IT, red = need help.

Teaching notes by part

Why this session matters. Sessions 1–3 used tidyverse functions that hide how R stores data. This session opens the hood. Vectors and vectorization explain why mutate() works on a whole column at once; base-R subsetting is everywhere in other people’s code, help pages, and Stack Overflow answers, and learners need it to write functions in Session 8; and reshaping is needed for many summaries and plots.

Part 2 — Vectors and types. Point out that c(1, 2, "unknown") is exactly why one “unknown” turned the whole age column into text in Session 1. Show that ages and the other objects appear in the Variables pane, which also shows each vector’s type and length.

Part 3 — Vectorization. Stress that mutate() is just a vectorized calculation on a column — no loop needed. Point out that sum()/mean() of a comparison counts/gives proportions; they will use mean(fever == "yes") * 100 inside summarise() in Session 5.

Part 4 — Subsetting with base R. Rows first, then columns — have learners say it out loud. A common slip is forgetting the comma (df[1:3] picks columns). Spend time on the NA warning box: nrow(linelist[linelist$temp > 40, ]) gives 244, nrow(filter(linelist, temp > 40)) gives 93 — base R adds a row of NAs for each of the 151 missing temperatures. TRY IT 4 (c) repeats this trap on the measles data (225 vs 196, 29 missing ages), so let learners discover it before you explain.

Part 5 — Lists. The one data structure the tidyverse sessions never show, and the one learners hit first in real analyses: every test and model result is a list, and so is every Bioconductor object. Use the box analogy: [[ ]] takes the item out of the box, [ ] gives a smaller box. Show is.list(linelist) — the “aha” that a data frame is a list of columns explains $, [[ ]], and (in Session 8) why map() runs once per column. For t.test(), say explicitly that the statistics come later; the point here is the shape of the result. Expect the “exactly 2 levels” error if someone forgets to filter out “Unknown” — it’s a useful mistake.

Part 6 — Tidy data and pivot_longer(). Point out that “observation” depends on the question: wide symptoms are fine for one symptom at a time, but long format lets one count() handle all five. nrow(symptoms_long) is 32255 = 6451 patients × 5 symptoms. The plot is a preview of Session 6 — don’t explain ggplot yet.

Part 7 — pivot_wider(). Emphasize the rule of thumb: long for R, wide for people. Mention values_fill = 0 (the hospital × outcome table has no empty combinations, so it isn’t needed here).

Wrap-up. Today: vectors and types, c(), class(), vectorized maths and comparisons, sum()/mean() of TRUE/FALSE, [ ], $, [[ ]], lists and str(), test results as lists, tidy data, pivot_longer(), pivot_wider(). Next is Session 5 (Summarizing: count(), group_by() + summarise(), tabyl(), Table 1); Session 8 returns to vectors and subsetting when writing functions and loops.

TRY IT answers at a glance

The numbers match the TRY IT numbers in the lesson, the student script, and the solutions file. Follow-along prompts are numbered by part (Follow along 5.3 = Part 5, third prompt).

TRY IT Answer
1. Import the measles data measles <- import(here("processed_data", "measles_final.rds"), trust = TRUE) — 1163 rows, 14 columns
2. Types in the measles data "numeric", "character", "Date"; length 1163
3. Vectorized calculations a) starts 100.94, 101.12; b) 565 cases ≥ 39 °C; c) 0.238 (23.8%) vaccinated
4. Subsetting the measles data a) first ID MS-0931; b) 38.75; c) base R 225 vs filter() 196 — 29 NA rows from missing ages; fix with & !is.na(measles$age_years)
5. A test result is a list t.test(temp_c ~ vaccinated, data = filter(measles, vaccinated != "Unknown")); p-value 0.565; means 38.80 (No) and 38.84 (Yes)
6. Measles symptoms long 2326 rows (1163 × 2); fever yes 1064, rash yes 976
7. District × vaccination table 6 rows (5 districts + Unknown), columns No / Unknown / Yes; North 205 / 49 / 88