01 / 21

Lists in
R Programming

Master R's most flexible data structure: create lists that hold mixed types, and access elements with \(\texttt{[]}\), \(\texttt{[[ ]]}\), and \(\texttt{\$}\).

R Programming

What Is a List?

In R, a list is an ordered collection of elements that can each hold a different data type: numbers, strings, vectors, data frames, or even other lists. Unlike vectors, which require all elements to be the same type, lists are R's most flexible container.

Why Lists Matter

Mixed Types
Store numbers, strings, logicals, and more in a single object.
Nested Data
Lists can contain other lists, enabling complex hierarchical structures.
Function Output
Many R functions (like \(\texttt{lm()}\) and \(\texttt{t.test()}\)) return their results as lists.

Lists vs Vectors

Atomic Vector 1 2 3 4 All same type (numeric) List 42 numeric "hello" character c(1, 2, 3) vector TRUE logical Mixed types allowed

Creating a List

# Create a named list student <- list( name = "Alice", age = 17, grades = c(95, 88, 92), active = TRUE )

Three Ways to Access Elements

student $name = "Alice" $age = 17 $grades = c(95,88,92) $active = TRUE [ ] Single Bracket Returns a sub-list (still a list) [[ ]] Double Bracket Returns the element (unwrapped value) $ Dollar Sign Same as [[ ]] but uses name directly These are the three fundamental list access operators in R

\(\texttt{[]}\) : Single Bracket

# Single bracket returns a SUB-LIST student[1] # list with $name = "Alice" student["age"] # list with $age = 17 student[1:2] # list with $name and $age # Check the type class(student[1]) # "list"

\(\texttt{[[ ]]}\) : Double Bracket

# Double bracket extracts the ACTUAL element student[[1]] # "Alice" (character) student[["age"]] # 17 (numeric) student[[3]] # c(95, 88, 92) (vector) # Check the type class(student[[1]]) # "character"

\(\texttt{\$}\) : Dollar Sign Operator

# Dollar sign accesses by NAME (no quotes needed) student$name # "Alice" student$grades # c(95, 88, 92) student$active # TRUE # Equivalent to: student[["name"]] # same result

\(\texttt{[]}\) vs \(\texttt{[[ ]]}\) : The Key Difference

student[1] still a list $name "Alice" class(): "list" Can select multiple: [1:2] vs student[[1]] "Alice" raw value class(): "character" One element only

How to Choose an Operator

1
Need multiple elements? Use \(\texttt{[]}\) with a vector of indices or names, e.g. \(\texttt{my\_list[1:3]}\).
2
Need one raw value? Use \(\texttt{[[ ]]}\) with an index or name, e.g. \(\texttt{my\_list[["age"]]}\).
3
Named element and want brevity? Use \(\texttt{\$}\) as a shortcut for \(\texttt{[[ ]]}\), e.g. \(\texttt{my\_list\$age}\).
4
Using a variable as the name? You must use \(\texttt{[[ ]]}\) because \(\texttt{\$}\) does not evaluate variables.

Example 1: Building a List

# Build a list about a book book <- list( title = "1984", author = "George Orwell", year = 1949, genres = c("dystopian", "political") ) # Access the title (raw value) book$title # "1984" # Get a sub-list of title and author book[c("title", "author")]

Example 2: Nested Access

# Access the second genre book$genres[2] # "political" book[["genres"]][2] # "political" book[[4]][2] # "political" # Step by step: # 1. book$genres → c("dystopian", "political") # 2. [2] → "political"

Example 3: Modifying Lists

# Add a new element book$pages <- 328 # Update an existing element book[["year"]] <- 1948 # Remove an element (set to NULL) book$genres <- NULL # Check the structure str(book)

Common Mistakes

✕ Using \(\texttt{[]}\) When You Need \(\texttt{[[ ]]}\)
Writing \(\texttt{student[1] + 5}\) causes an error because \(\texttt{[]}\) returns a list, not a number. Use \(\texttt{student[[1]] + 5}\) instead.
✕ Using \(\texttt{\$}\) with a Variable
If \(\texttt{key <- "name"}\), then \(\texttt{student\$key}\) looks for an element literally named "key". Use \(\texttt{student[[key]]}\) instead.
✕ Forgetting Names
Trying \(\texttt{student\$name}\) on a list with no names returns \(\texttt{NULL}\) silently: no error, just missing data. Always name your list elements.

Summary Comparison

Feature \(\texttt{[]}\) \(\texttt{[[ ]]}\) \(\texttt{\$}\)
Returns Sub-list Raw value Raw value
Multiple elements ✓ Yes ✕ No ✕ No
By index ✓ Yes ✓ Yes ✕ No
By name ✓ Yes ✓ Yes ✓ Yes
With variable ✓ Yes ✓ Yes ✕ No
Quotes needed For names For names Never

Question 1 of 5

Given \(\texttt{my\_list <- list(a = 10, b = "hello", c = TRUE)}\), what does \(\texttt{my\_list[1]}\) return?

Question 2 of 5

Which operator would you use to extract the raw value of a named element from a list using a variable that stores the element's name?

Question 3 of 5

Given \(\texttt{info <- list(x = c(5, 10, 15), y = "test")}\), what does \(\texttt{info\$x[2]}\) return?

Question 4 of 5

What is the correct way to add a new element called \(\texttt{score}\) with value \(\texttt{95}\) to an existing list called \(\texttt{record}\)?

Question 5 of 5

What is the key difference between a list and an atomic vector in R?