AnyCompiler

Named Lists and Structures

A list with named elements, created the same way as the previous lesson's lst, works as a lightweight record: lst$field reads a named element directly, and the same $ syntax on the left of <- updates it. This is the pattern you'll see for anything record-shaped in R, from a single person's details to one row's worth of configuration.

person <- list(name = "Ada", age = 36, skills = c("math", "programming"))
print(person$name)
print(person$skills)
person$age <- 37
print(person$age)
Output
[1] "Ada"
[1] "math"        "programming"
[1] 37

person$skills returned a plain character vector of length 2 — a list can perfectly well hold a vector as one of its named elements, not just single values. person$age <- 37 reassigned that one field in place, leaving name and skills untouched. Next: data frames — R's central tabular data structure, where this same $ syntax reaches into a column.

Practice exercise

Create book <- list(title = "R Basics", pages = 120). Print book$title, then update book$pages to 150 and print it.