Unit 04.05: Long to compute, wide to read
Long is the shape for computing. Wide is the shape for reading. Most analyses need both, at different moments.
One reshape out, one reshape back
pivot_longer() takes columns whose *names* are data and turns them into two columns: one holding the old names, one holding the values. pivot_wider() does the reverse.
The arguments worth knowing early are names_to and values_to, which name the new columns, and names_pattern, which extracts just the part of the old column name that is actually the value — so jan_visits yields jan rather than the whole label.
The reliable check is a round trip: pivot long, pivot back, and confirm you have the original table. If you do not, the reshape lost or duplicated something.
This block pivots a wide table to long, back to wide, and compares.
suppressPackageStartupMessages({library(tidyr); library(dplyr); library(tibble)})
wide <- tibble(ward = c("North", "South"),
jan_visits = c(412, 388),
feb_visits = c(455, 401))
long <- wide |>
pivot_longer(cols = ends_with("_visits"),
names_to = "month",
names_pattern = "(\\w+)_visits",
values_to = "visits")
print(long)
# Long is the shape for computing. Wide is the shape for reading.
back <- long |>
pivot_wider(names_from = month, values_from = visits, names_glue = "{month}_visits")
cat("\nRound trip returns the original:", identical(wide, back), "\n\n")
cat("Grouped work is trivial in the long shape:\n")
print(long |> group_by(month) |> summarise(total = sum(visits), .groups = "drop"))
cat("\nThe same sum in the wide shape means naming every column by hand.\n")
The long form has 4 rows and the month column holds jan and feb, extracted by the pattern from jan_visits and feb_visits. The round trip returns TRUE — identical to the original. Then the payoff: totalling by month is one group_by() in the long shape, giving feb 856 and jan 800. In the wide shape the same answer means naming every month column by hand, and editing that list whenever a month is added.
The mistake this prevents
The mistake is pivoting wider too early because the table looks better, then finding every subsequent grouping needs the long form again.
Takeaway
Pivot to long as soon as you import, do all the computation there, and pivot to wide once at the end for presentation. Verify a reshape with a round-trip comparison.
