Advanced Data Structures
Nested Records
Pick One Item
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.
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 = "")
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_indexrecords ← 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 recordsrecordsstudent ← 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_indexlabel ← Bo:92
3student <- records[[student_index]]4label <- paste(student$name, student$score, sep = ":")5cat(label, "\n", sep = "")values this stepBo:92labelBo score 92studentcat(label, " ", sep = "")
4label <- paste(student$name, student$score, sep = ":")5cat(label, "\n", sep = "")outputBo:92values this stepBo:92label
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_indexrecords ← 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 recordsrecordsstudent ← 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_indexlabel ← Ana:80
3student <- records[[student_index]]4label <- paste(student$name, student$score, sep = ":")5cat(label, "\n", sep = "")values this stepAna:80labelAna score 80studentcat(label, " ", sep = "")
4label <- paste(student$name, student$score, sep = ":")5cat(label, "\n", sep = "")outputAna:80values this stepAna:80label
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_indexrecords ← 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 recordsrecordsstudent ← 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_indexlabel ← Cy:75
3student <- records[[student_index]]4label <- paste(student$name, student$score, sep = ":")5cat(label, "\n", sep = "")values this stepCy:75labelCy score 75studentcat(label, " ", sep = "")
4label <- paste(student$name, student$score, sep = ":")5cat(label, "\n", sep = "")outputCy:75values 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.