Tidy Data Patterns
Wide to Long
Naming the Score Column
A wide table stores different measurements in separate columns. A long table moves the measurement name into data.
Program
Play the script to choose a score column and build tidy rows with a subject label.
wide_to_long.R
Replay: real traced execution (multi-file project)
wide <- data.frame(name = c("Ada", "Lin"), math = c(9, 7), science = c(8, 10))
subject_index <- 1
subject <- c("math", "science")[subject_index]
long <- data.frame(name = wide$name, subject = subject, score = wide[[subject]])
top <- long$name[which.max(long$score)]
cat(top, "\n", sep = "")
wide <- data.frame(name = c("Ada", "Lin"), math = c(9, 7), science = c(8, 10))
subject_index <- 2
subject <- c("math", "science")[subject_index]
long <- data.frame(name = wide$name, subject = subject, score = wide[[subject]])
top <- long$name[which.max(long$score)]
cat(top, "\n", sep = "")
wide ← 2 rows x 3 cols
1wide <- data.frame(name = c("Ada", "Lin"), math = c(9, 7), science = c(8, 10))2subject_index <- 1values this step2 rows x 3 colswidesubject_index ← 1
1wide <- data.frame(name = c("Ada", "Lin"), math = c(9, 7), science = c(8, 10))2subject_index <- 13subject <- c("math", "science")[subject_index]values this step1subject_indexsubject ← math
2subject_index <- 13subject <- c("math", "science")[subject_index]4long <- data.frame(name = wide$name, subject = subject, score = wide[[subject]])values this stepmathsubject1subject_indexlong ← 2 rows x 3 cols
3subject <- c("math", "science")[subject_index]4long <- data.frame(name = wide$name, subject = subject, score = wide[[subject]])5top <- long$name[which.max(long$score)]values this step2 rows x 3 colslongmathsubject9, 7wide$mathtop ← Ada
4long <- data.frame(name = wide$name, subject = subject, score = wide[[subject]])5top <- long$name[which.max(long$score)]6cat(top, "\n", sep = "")values this stepAdatop9, 7long$scorecat(top, " ", sep = "")
5top <- long$name[which.max(long$score)]6cat(top, "\n", sep = "")outputAdavalues this stepAdatop
wide ← 2 rows x 3 cols
1wide <- data.frame(name = c("Ada", "Lin"), math = c(9, 7), science = c(8, 10))2subject_index <- 2values this step2 rows x 3 colswidesubject_index ← 2
1wide <- data.frame(name = c("Ada", "Lin"), math = c(9, 7), science = c(8, 10))2subject_index <- 23subject <- c("math", "science")[subject_index]values this step2subject_indexsubject ← science
2subject_index <- 23subject <- c("math", "science")[subject_index]4long <- data.frame(name = wide$name, subject = subject, score = wide[[subject]])values this stepsciencesubject2subject_indexlong ← 2 rows x 3 cols
3subject <- c("math", "science")[subject_index]4long <- data.frame(name = wide$name, subject = subject, score = wide[[subject]])5top <- long$name[which.max(long$score)]values this step2 rows x 3 colslongsciencesubject8, 10wide$sciencetop ← Lin
4long <- data.frame(name = wide$name, subject = subject, score = wide[[subject]])5top <- long$name[which.max(long$score)]6cat(top, "\n", sep = "")values this stepLintop8, 10long$scorecat(top, " ", sep = "")
5top <- long$name[which.max(long$score)]6cat(top, "\n", sep = "")outputLinvalues this stepLintop
wide table
The original table has one score column per subject.
long table
`subject` stores the measurement name and `score` stores its value.
column lookup
`wide[[subject]]` selects a column by name.