Pivoting: Long & Wide
In Lesson 1 you stitched two tables together with joins. But even a single, complete table can be in the wrong shape for the job in front of you.
Maya, who runs a small bakery, keeps her week of loaf sales on a whiteboard the way any of us would: one row per item, one column per day.
| item | Mon | Tue | Wed |
|---|---|---|---|
| Sourdough | 18 | 22 | 25 |
| Bagel | 40 | 38 | 44 |
| Croissant | 15 | 19 | 12 |
Lovely to read. But the moment she wants to plot sales over the week, or average them, this shape fights her: the variable day is hidden in the column headers, not in a column she can point at. Reshaping fixes that, and reshaping is this whole lesson.
By the end you will be able to:
- Tell wide shape from long (tidy) shape, and say which one a tool wants
- Reshape wide to long with
pivot_longer(), and long back to wide withpivot_wider() - Bundle a group's rows into one cell with
nest(), and unpack them again withunnest()
Prerequisites: you can run R, and you know a tibble, the pipe %>% and the dplyr verbs from The dplyr Verbs. Drag the toggle below to see where we are heading.
The same numbers, two shapes
Maya's whiteboard holds exactly nine numbers (three items across three days). There are two natural ways to lay them out, and they hold the identical information.
Wide is her whiteboard: one row per item, and one column for each day. The day lives in the header.
| item | Mon | Tue | Wed |
|---|---|---|---|
| Sourdough | 18 | 22 | 25 |
| Bagel | 40 | 38 | 44 |
| Croissant | 15 | 19 | 12 |
Long (the tidy shape) gives every one of those nine numbers its own row, and names the two things that describe it, the day and the units, as proper columns:
| item | day | units |
|---|---|---|
| Sourdough | Mon | 18 |
| Sourdough | Tue | 22 |
| Sourdough | Wed | 25 |
| Bagel | Mon | 40 |
| Bagel | Tue | 38 |
| Bagel | Wed | 44 |
| Croissant | Mon | 15 |
| Croissant | Tue | 19 |
| Croissant | Wed | 12 |
Same nine sales, retold. The long table is taller and looks more repetitive, but notice what it gained: day is now a real column, and so is units. That is the difference that matters, and the next move is how you get from the first shape to the second.