Data Analysis
Reshape
Wide to Long
Long data stores repeated measurements in rows. This shape is useful for grouped summaries and plotting.
Program
Play the script to turn morning/evening columns into period rows.
reshape_long.R
wide <- data.frame(day = c("Mon", "Tue"), morning = c(3, 4), evening = c(5, 6))
long <- data.frame(day = rep(wide$day, 2), period = rep(c("morning", "evening"), each = 2), value = c(wide$morning, wide$evening))
total <- sum(long$value)
cat(total, "\n", sep = "")
wide data
Wide data stores repeated measurements in separate columns.
long data
Long data stores measurement type and value in rows.
rep
`rep` repeats values to build aligned columns.