Home » R Lists Tutorial with Examples

R Lists Tutorial with Examples

In R, a list is a versatile data structure that can store elements of different types, such as numbers, strings, vectors, and even other lists.

Unlike vectors, which can only contain elements of the same type, lists in R can hold a variety of data types, making them one of the most flexible structures in R.

This tutorial will cover:

Let’s explore each of these topics with code examples.


1. Creating a List

A list in R can be created using the list() function. You can combine different data types like numeric values, strings, vectors, and even other lists in a single list.

Example:

# Creating a list with different data types
my_list <- list(name = "John", age = 25, numbers = c(1, 2, 3), is_student = TRUE)

print(my_list)
  • This list contains a string (name), a numeric value (age), a vector (numbers), and a logical value (is_student).

Output:

$name
[1] "John"

$age
[1] 25

$numbers
[1] 1 2 3

$is_student
[1] TRUE

2. Accessing List Elements

You can access elements of a list using either indexing or named elements. R provides several ways to extract values from a list:

  • Using double square brackets ([[ ]) to extract a single element.
  • Using single square brackets ([ ]) to extract a sublist.
  • Using the $ operator to access named elements.

Example (Accessing by Index):

# Access the first element of the list (name)
name <- my_list[[1]]
print(name)  # Outputs: "John"

# Access the vector element in the list
numbers <- my_list[[3]]
print(numbers)  # Outputs: 1 2 3

Example (Accessing by Name):

# Access the "age" element using $
age <- my_list$age
print(age)  # Outputs: 25

# Access the "numbers" element using the name in double brackets
numbers <- my_list[["numbers"]]
print(numbers)  # Outputs: 1 2 3
  • The $ operator is useful when you want to access elements by their names.

3. Modifying List Elements

You can modify list elements by assigning new values to specific elements of the list.

Example:

# Modify the "age" element
my_list$age <- 30
print(my_list$age)  # Outputs: 30

# Modify the "numbers" element
my_list$numbers <- c(4, 5, 6)
print(my_list$numbers)  # Outputs: 4 5 6
  • This modifies the existing values in the list.

4. Adding Elements to a List

You can add new elements to an existing list by assigning a value to a new index or name.

Example:

# Add a new element (city) to the list
my_list$city <- "New York"
print(my_list)

Output:

$name
[1] "John"

$age
[1] 30

$numbers
[1] 4 5 6

$is_student
[1] TRUE

$city
[1] "New York"
  • The new element city is added to the list.

5. Removing Elements from a List

You can remove elements from a list by assigning NULL to the element you want to remove.

Example:

# Remove the "is_student" element from the list
my_list$is_student <- NULL
print(my_list)

Output:

$name
[1] "John"

$age
[1] 30

$numbers
[1] 4 5 6

$city
[1] "New York"
  • The is_student element is removed from the list.

6. Merging Lists

You can combine two or more lists into a single list using the c() function.

Example:

# Create two lists
list1 <- list(a = 1, b = 2)
list2 <- list(c = 3, d = 4)

# Merge the two lists
merged_list <- c(list1, list2)
print(merged_list)

Output:

$a
[1] 1

$b
[1] 2

$c
[1] 3

$d
[1] 4
  • The two lists are merged into a single list.

7. Applying Functions to Lists

You can apply functions to each element of a list using the lapply() and sapply() functions.

  • lapply() returns a list.
  • sapply() simplifies the result into a vector or matrix when possible.

Example (Using lapply()):

# Define a list of numbers
num_list <- list(a = 1, b = 2, c = 3)

# Apply a function to square each element in the list
squared <- lapply(num_list, function(x) x^2)
print(squared)

Output:

$a
[1] 1

$b
[1] 4

$c
[1] 9

Example (Using sapply()):

# Apply the same function using sapply() (returns a vector)
squared_vector <- sapply(num_list, function(x) x^2)
print(squared_vector)

Output:

a b c 
1 4 9 
  • sapply() simplifies the output to a vector, whereas lapply() returns a list.

8. Converting Lists to Vectors

You can convert a list to a vector using the unlist() function. This function flattens the list into a vector.

Example:

# Convert a list to a vector
list_example <- list(a = 1, b = 2, c = 3)
vector_result <- unlist(list_example)
print(vector_result)

Output:

a b c 
1 2 3 
  • The list is converted into a named vector.

9. Nested Lists

Lists can contain other lists, creating nested lists. You can access elements of a nested list using indexing or names.

Example:

# Create a nested list
nested_list <- list(name = "John", details = list(age = 30, city = "New York"))

# Access elements from the nested list
print(nested_list$details$age)  # Outputs: 30
print(nested_list$details$city)  # Outputs: "New York"
  • In this case, details is a list within the main list, and you can access its elements using $ or double square brackets.

Summary of R List Operations

Operation Description
list() Creates a list.
[[ ]] Access a single element by index.
$ Access an element by name.
[] Access a sublist.
lapply() and sapply() Apply a function to each element of a list.
c() Combine or merge lists.
unlist() Convert a list to a vector.
NULL Remove an element from a list.
Nested Lists Lists within lists (access with $ or [[ ]]).

Conclusion

Lists are one of the most powerful and flexible data structures in R.

They allow you to store and manipulate complex data types in an organized way.

By mastering the operations on lists—such as accessing, modifying, merging, and applying functions—you can handle complex data analysis tasks efficiently.

Key takeaways:

  • Lists can store multiple data types.
  • You can access list elements by index or name.
  • Functions like lapply(), sapply(), and unlist() are useful for working with lists.
  • Lists can be nested, and you can store other lists within a list.

By using lists effectively, you can manage and process diverse data in R, making them an essential tool for any R programmer.

 

You may also like