Lists in R
In this lesson, you will learn about lists in R. Lists are powerful data structures that allow you to store multiple values of different data types together.
Unlike vectors, lists do not restrict you to a single data type. This makes lists very useful when working with complex or real-world data.
What Is a List?
A list is a collection of elements where each element can be of a different data type. A list can store numbers, text, logical values, vectors, or even other lists.
This flexibility makes lists one of the most important structures in R programming.
Creating a List
Lists are created using the list() function.
Each value inside the list becomes a separate element.
You can give names to list elements to make them easier to access.
data <- list(
number = 100,
text = "R Programming",
status = TRUE
)
data
Here, the list stores numeric, character, and logical values together.
Lists vs Vectors
Vectors store multiple values of the same data type. Lists can store values of different data types in one structure.
When your data becomes complex, lists are the better choice.
Accessing List Elements by Position
Each element in a list has a position, starting from 1. You can access elements using double square brackets.
This method extracts the actual value stored in the list.
data[[1]]
data[[2]]
Using double brackets ensures you get the element itself, not a sublist.
Accessing List Elements by Name
If a list element has a name, you can access it using the name. This makes your code more readable and clear.
data$number
data$text
Named access is preferred when working with structured data.
Storing Vectors Inside a List
Lists can store vectors as elements. This allows you to group related data together.
This is very common in data analysis workflows.
scores <- list(
math = c(80, 85, 90),
science = c(78, 82, 88)
)
scores
Each subject is stored as a separate vector inside the list.
Modifying List Elements
You can change the value of an existing list element. You can also add new elements to a list.
Lists are flexible and easy to update.
data$number <- 250
data$new_item <- "Added later"
data
This ability makes lists useful for dynamic data storage.
Checking the Length of a List
The length() function returns the number of elements in a list.
This counts top-level elements only.
length(data)
Nested elements are not counted separately.
Nested Lists
A list can contain another list inside it. This is called a nested list.
Nested lists are useful for representing structured or hierarchical data.
project <- list(
title = "R Analysis",
details = list(
duration = "3 months",
status = TRUE
)
)
project
Nested lists help organize complex information logically.
Why Lists Are Important
Lists are widely used in R for storing model outputs, datasets, and results.
Many R functions return results as lists. Understanding lists helps you work confidently with real data.
📝 Practice Exercises
Exercise 1
Create a list that stores a number, text, and a logical value.
Exercise 2
Access the second element of a list using its position.
Exercise 3
Create a list containing two numeric vectors.
Exercise 4
Add a new element to an existing list.
✅ Practice Answers
Answer 1
example <- list(50, "R Language", FALSE)
example
Answer 2
example[[2]]
Answer 3
values <- list(
a = c(1, 2, 3),
b = c(4, 5, 6)
)
values
Answer 4
example$new_value <- 999
example
What’s Next?
Now that you understand lists, you are ready to work with matrices in R.
In the next lesson, you will learn how to store and manipulate data in rows and columns using matrices.