Lists, Matrices, and Tables
Lists
Grouping Related Values
Lists hold related values that do not need to share a type or length.
Program
Play the script to group a name with scores and build a label from both.
lists.R
Replay: real traced execution (multi-file project)
person <- list(name = "Ada", scores = c(9, 10))
best <- max(person$scores)
label <- paste(person$name, best)
cat(label, "\n", sep = "")
person ← name=Ada, scores=9,10
1person <- list(name = "Ada", scores = c(9, 10))2best <- max(person$scores)values this stepname=Ada, scores=9,10personbest ← 10
1person <- list(name = "Ada", scores = c(9, 10))2best <- max(person$scores)3label <- paste(person$name, best)values this step10best9, 10person$scoreslabel ← Ada 10
2best <- max(person$scores)3label <- paste(person$name, best)4cat(label, "\n", sep = "")values this stepAda 10labelAdaperson$name10bestcat(label, " ", sep = "")
3label <- paste(person$name, best)4cat(label, "\n", sep = "")outputAda 10values this stepAda 10label
Follow the List
persongroups the nameAdawith scores9and10.person$scoresreads the score vector from the list.max(person$scores)finds the best score,10.paste(person$name, best)buildsAda 10.- The script prints
Ada 10. | list part | value | | --- | --- | |person$name| Ada | |person$scores| 9, 10 | |best| 10 | |label| Ada 10 |
list
`list` groups named values together.
$
`person$scores` reads one named list element.
max
`max` returns the largest numeric value.
Exercise: lists.R
Reproduce Ada 10, then change one score in person$scores and predict the new best value before running it.