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
wide <- data.frame(name = c("Ada", "Lin"), math = c(9, 7), science = c(8, 10))
subject_index <-
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 <-
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 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.