DS 7: dplyr II + tidyr

Week 7 · Tuesday, Nov 10 · 9:50-10:55 am · Self-guided

Dr. Marcela Alfaro Córdoba

This morning

On your own, no AI:

  1. Lesson 14: dplyr II: groups and joins: 5 exercises
  2. Lesson 15: Reshaping Data with tidyr: 4 exercises

Keep a script as you go: week07/ds07_notes.R, one heading per exercise, your final answer under each.

Important

3 points: 2 for the PDFs on Canvas, 1 for this afternoon’s check.

Same rules as always

  • Allowed: ?help pages, the lesson pages, Thursday’s slides, your notes, asking me
  • Not allowed: any chatbot, Positron Assistant, inline suggestions, a classmate’s code

Stuck for more than 10 minutes on one exercise? Raise your hand.

Lesson 15 is your first pivot

Same numbers, different shape. Ask: what does one row stand for?

gapminder |>
  pivot_longer(cols = c(lifeExp, pop, gdpPercap),   # the columns to stack
               names_to = "metric",                 # their names go here
               values_to = "value")                 # their values go here
  • pivot_longer(): more rows, fewer columns. One row per single measurement
  • pivot_wider(): fewer rows, more columns. The reverse
  • library(tidyr) at the top of your script, next to library(dplyr)

Before you pivot, predict nrow() and ncol() of the result. 1704 country-years × 3 metrics stacked is how many rows?

At 10:45: one last comment

At the bottom of week07/ds07_notes.R:

# The hardest part, or something I didn't understand:
#

Be specific. “pivots” doesn’t help; “why Exercise 4 gives so many NAs” does. You’ll use it this afternoon.

Before you go

Canvas: both PDFs by Wednesday 11:59 pm. Printing is optional.

1:30 pm: bring your laptop, charged. You need data/survey_wave2.csv and data/sites.csv from last week.