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.

subject_index
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 = "")
  1. wide ← 2 rows x 3 cols

    1wide <- data.frame(name = c("Ada", "Lin"), math = c(9, 7), science = c(8, 10))2subject_index <- 1
    values this step2 rows x 3 colswide
  2. subject_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_index
  3. subject ← math

    2subject_index <- 13subject <- c("math", "science")[subject_index]4long <- data.frame(name = wide$name, subject = subject, score = wide[[subject]])
    values this stepmathsubject1subject_index
  4. long ← 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$math
  5. top ← 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$score
  6. cat(top, " ", sep = "")

    5top <- long$name[which.max(long$score)]6cat(top, "\n", sep = "")
    outputAda
    values this stepAdatop
  1. wide ← 2 rows x 3 cols

    1wide <- data.frame(name = c("Ada", "Lin"), math = c(9, 7), science = c(8, 10))2subject_index <- 2
    values this step2 rows x 3 colswide
  2. subject_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_index
  3. subject ← science

    2subject_index <- 23subject <- c("math", "science")[subject_index]4long <- data.frame(name = wide$name, subject = subject, score = wide[[subject]])
    values this stepsciencesubject2subject_index
  4. long ← 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$science
  5. top ← 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$score
  6. cat(top, " ", sep = "")

    5top <- long$name[which.max(long$score)]6cat(top, "\n", sep = "")
    outputLin
    values 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.