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 = "")
  1. person ← name=Ada, scores=9,10

    1person <- list(name = "Ada", scores = c(9, 10))2best <- max(person$scores)
    values this stepname=Ada, scores=9,10person
  2. best ← 10

    1person <- list(name = "Ada", scores = c(9, 10))2best <- max(person$scores)3label <- paste(person$name, best)
    values this step10best9, 10person$scores
  3. label ← Ada 10

    2best <- max(person$scores)3label <- paste(person$name, best)4cat(label, "\n", sep = "")
    values this stepAda 10labelAdaperson$name10best
  4. cat(label, " ", sep = "")

    3label <- paste(person$name, best)4cat(label, "\n", sep = "")
    outputAda 10
    values this stepAda 10label

Follow the List

  1. person groups the name Ada with scores 9 and 10.
  2. person$scores reads the score vector from the list.
  3. max(person$scores) finds the best score, 10.
  4. paste(person$name, best) builds Ada 10.
  5. 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.