Lesson 11 of 16

Lists

Lists

Unlike vectors, lists can hold elements of different types:

person <- list(name = "Alice", age = 30, active = TRUE)

Accessing Elements

There are three ways to access list elements:

person <- list(name = "Alice", age = 30)

# By name with $
cat(person$name, "\n")      # Alice

# By name with [[]]
cat(person[["age"]], "\n")   # 30

# By position with [[]]
cat(person[[1]], "\n")       # Alice

Note the double brackets [[]] -- single brackets [] return a sub-list, not the element itself.

Modifying Lists

You can add or modify elements:

person <- list(name = "Alice", age = 30)
person$email <- "alice@example.com"  # Add element
person$age <- 31                      # Modify element

Nested Lists

Lists can contain other lists:

team <- list(
  lead = list(name = "Alice", role = "Lead"),
  dev = list(name = "Bob", role = "Developer")
)
cat(team$lead$name, "\n")  # Alice

Your Task

Create a list called book with elements title ("The R Book"), pages (1060), and available (TRUE). Print each element on its own line using $ notation.

R runtime loading...
Loading...
Click "Run" to execute your code.