Unit 04.00: When the column names are data
Tidy data is one rule with an unreasonable payoff: every variable is a column, every observation is a row.
When the column names are data, the table is untidy
The commonest untidy shape is a table with Jan and Feb as column names. Those are not variables; they are *values* of a variable called month, and putting them in the header means month cannot be filtered, grouped or joined like anything else.
The cost is felt when the data changes. Adding March to a wide table means editing every piece of code that names the months. In the long form it means two more rows and no code change at all.
The tidyverse verbs assume tidy input. Fighting them is nearly always a sign that the reshape has not happened yet.
This block shows the untidy shape and the tidy one side by side.
suppressPackageStartupMessages({library(tidyr); library(tibble)})
# Untidy: the column NAMES are data. "Jan" and "Feb" are values of a variable.
wide <- tibble(ward = c("North", "South", "East"),
Jan = c(412, 388, 502),
Feb = c(455, 401, 498))
print(wide)
tidy <- pivot_longer(wide, cols = c(Jan, Feb),
names_to = "month", values_to = "visits")
cat("\nTidy: one variable per column, one observation per row\n")
print(tidy)
cat("\nRows:", nrow(wide), "->", nrow(tidy), " Columns:", ncol(wide), "->", ncol(tidy), "\n")
cat("Now 'month' can be filtered, grouped and joined like any other variable.\n")
cat("In the wide form, adding March means editing every piece of code.\n")
The wide table is 3 rows by 3 columns; the tidy one is 6 by 3. The row count doubled because each ward now has one row per month, and month has become an ordinary column holding Jan and Feb as values. That is the whole transformation, and it is what makes the grouping in the next lesson possible.
The mistake this prevents
The mistake is treating the wide layout as the finished shape because it reads well. Wide is a presentation format. Do the work in the long form and pivot back at the very end.
Takeaway
If a column name is a date, a category or a year, the table is untidy. Pivot to long for computation, and pivot back to wide only for the final table a person will read.
