A list of lists can represent several records without forcing every field into one vector type.

Program

Play the script to choose a record and show its name plus score.

student_index
nested_records.R
Replay: real traced execution (multi-file project)
student_index <- 2
records <- list(list(name = "Ana", score = 80), list(name = "Bo", score = 92), list(name = "Cy", score = 75))
student <- records[[student_index]]
label <- paste(student$name, student$score, sep = ":")
cat(label, "\n", sep = "")
student_index <- 1
records <- list(list(name = "Ana", score = 80), list(name = "Bo", score = 92), list(name = "Cy", score = 75))
student <- records[[student_index]]
label <- paste(student$name, student$score, sep = ":")
cat(label, "\n", sep = "")
student_index <- 3
records <- list(list(name = "Ana", score = 80), list(name = "Bo", score = 92), list(name = "Cy", score = 75))
student <- records[[student_index]]
label <- paste(student$name, student$score, sep = ":")
cat(label, "\n", sep = "")
  1. student_index ← 2

    1student_index <- 22records <- list(list(name = "Ana", score = 80), list(name = "Bo", score = 92), list(name = "Cy", score = 75))
    values this step2student_index
  2. records ← 3 student records

    1student_index <- 22records <- list(list(name = "Ana", score = 80), list(name = "Bo", score = 92), list(name = "Cy", score = 75))3student <- records[[student_index]]
    values this step3 student recordsrecords
  3. student ← name=Bo, score=92

    2records <- list(list(name = "Ana", score = 80), list(name = "Bo", score = 92), list(name = "Cy", score = 75))3student <- records[[student_index]]4label <- paste(student$name, student$score, sep = ":")
    values this stepname=Bo, score=92student2student_index
  4. label ← Bo:92

    3student <- records[[student_index]]4label <- paste(student$name, student$score, sep = ":")5cat(label, "\n", sep = "")
    values this stepBo:92labelBo score 92student
  5. cat(label, " ", sep = "")

    4label <- paste(student$name, student$score, sep = ":")5cat(label, "\n", sep = "")
    outputBo:92
    values this stepBo:92label
  1. student_index ← 1

    1student_index <- 12records <- list(list(name = "Ana", score = 80), list(name = "Bo", score = 92), list(name = "Cy", score = 75))
    values this step1student_index
  2. records ← 3 student records

    1student_index <- 12records <- list(list(name = "Ana", score = 80), list(name = "Bo", score = 92), list(name = "Cy", score = 75))3student <- records[[student_index]]
    values this step3 student recordsrecords
  3. student ← name=Ana, score=80

    2records <- list(list(name = "Ana", score = 80), list(name = "Bo", score = 92), list(name = "Cy", score = 75))3student <- records[[student_index]]4label <- paste(student$name, student$score, sep = ":")
    values this stepname=Ana, score=80student1student_index
  4. label ← Ana:80

    3student <- records[[student_index]]4label <- paste(student$name, student$score, sep = ":")5cat(label, "\n", sep = "")
    values this stepAna:80labelAna score 80student
  5. cat(label, " ", sep = "")

    4label <- paste(student$name, student$score, sep = ":")5cat(label, "\n", sep = "")
    outputAna:80
    values this stepAna:80label
  1. student_index ← 3

    1student_index <- 32records <- list(list(name = "Ana", score = 80), list(name = "Bo", score = 92), list(name = "Cy", score = 75))
    values this step3student_index
  2. records ← 3 student records

    1student_index <- 32records <- list(list(name = "Ana", score = 80), list(name = "Bo", score = 92), list(name = "Cy", score = 75))3student <- records[[student_index]]
    values this step3 student recordsrecords
  3. student ← name=Cy, score=75

    2records <- list(list(name = "Ana", score = 80), list(name = "Bo", score = 92), list(name = "Cy", score = 75))3student <- records[[student_index]]4label <- paste(student$name, student$score, sep = ":")
    values this stepname=Cy, score=75student3student_index
  4. label ← Cy:75

    3student <- records[[student_index]]4label <- paste(student$name, student$score, sep = ":")5cat(label, "\n", sep = "")
    values this stepCy:75labelCy score 75student
  5. cat(label, " ", sep = "")

    4label <- paste(student$name, student$score, sep = ":")5cat(label, "\n", sep = "")
    outputCy:75
    values this stepCy:75label
nested list A list can contain another list for each record.
record selection `records[[student_index]]` chooses one full record.
field access `student$name` and `student$score` read fields inside the chosen record.