R Lists
A list is a collection of objects in R that can hold different types of data, such as numbers, strings, vectors, another list, matrices, data frames, and so on; of course, it can also contain matrices and functions.
A list is a flexible data structure that can store and manipulate multiple types of data objects.
Creating a list
In R, lists are created with the list() function.
In the following example, we create a list containing strings, a vector, and a number:
Example
list_data
<-
list
(
"runoob"
,
"google"
,
c
(
11
,
22
,
33
)
,
123
,
51.23
,
119.1
)
(
list_data
)
The output of the code above is:
[[1]]
[1] "runoob"
[[2]]
[1] "google"
[[3]]
[1] 11 22 33
[[4]]
[1] 123
[[5]]
[1] 51.23
[[6]]
[1] 119.1
We can also use the c() function to create a list, or use it to combine multiple objects into a single list, for example:
my_list <- c(object1, object2, object3)
Example
Create a vector containing numbers
numbers
<-
c
(
1
,
2
,
3
,
4
,
5
)
Create a vector containing characters
characters
<-
c
(
"apple"
,
"banana"
,
"orange"
)
Combine two numeric vectors
merged_vector
<-
c
(
numbers,
c
(
6
,
7
,
8
)
)
Combine two character vectors
merged_characters
<-
c
(
characters,
c
(
"grape"
,
"melon"
)
)
We can use the names() function to assign names to the elements of a list:
Example
The list contains vectors, matrices, and a list
list_data
<-
list
(
c
(
"Google"
,
"Runoob"
,
"Taobao"
)
,
matrix
(
c
(
1
,
2
,
3
,
4
,
5
,
6
)
,
nrow
=
2
)
,
list
(
"runoob"
,
12.3
)
)
Set names for the list elements
names
(
list_data
)
<-
c
(
"Sites"
,
"Numbers"
,
"Lists"
)
Display the list
(
list_data
)
The output of the code above is:
$Sites
[1] "Google" "Runoob" "Taobao"
$Numbers
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
$Lists
$Lists[[1]]
[1] "runoob"
$Lists[[2]]
[1] 12.3
Accessing a list
Elements in a list can be accessed by index; if the names() elements are named using a function, we can also access them by their corresponding names:
Example
The list contains vectors, matrices, and a list
list_data
<-
list
(
c
(
"Google"
,
"Runoob"
,
"Taobao"
)
,
matrix
(
c
(
1
,
2
,
3
,
4
,
5
,
6
)
,
nrow
=
2
)
,
list
(
"runoob"
,
12.3
)
)
Set names for the list elements
names
(
list_data
)
<-
c
(
"Sites"
,
"Numbers"
,
"Lists"
)
Display the list
(
list_data
[
1
]
)
Access the third element of the list
(
list_data
[
3
]
)
Access the first vector element
(
list_data$Numbers
)
The output of the code above is:
$Sites
[1] "Google" "Runoob" "Taobao"
$Lists
$Lists[[1]]
[1] "runoob"
$Lists[[2]]
[1] 12.3
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
Manipulating list elements
We can add, delete, and update list elements, as shown in the following example:
Example
The list contains vectors, matrices, and a list
list_data
<-
list
(
c
(
"Google"
,
"Runoob"
,
"Taobao"
)
,
matrix
(
c
(
1
,
2
,
3
,
4
,
5
,
6
)
,
nrow
=
2
)
,
list
(
"runoob"
,
12.3
)
)
Set names for the list elements
names
(
list_data
)
<-
c
(
"Sites"
,
"Numbers"
,
"Lists"
)
Adding elements
list_data
[
4
]
<-
"new element"
(
list_data
[
4
]
)
Delete elements
list_data
[
4
]
<-
NULL
After deletion, the output is NULL
(
list_data
[
4
]
)
Update an element
list_data
[
3
]
<-
"I replaced the third element"
(
list_data
[
3
]
)
The output of the code above is:
[[1]]
[1] "新元素"
$
NULL
$Lists
[1] "我替换来第三个元素"
When iterating over a list with a for loop:
Example
Create a list containing numbers and characters
my_list
<-
list
(
1
,
2
,
3
,
"a"
,
"b"
,
"c"
)
Use a for loop to iterate over each element in the list
for
(
element
in
my_list
)
{
(
element
)
}
In the code above, the for loop iterates over each element in the list my_list in turn and stores each element in the variable element. We can then perform specific operations on each element within the loop body, such as printing the element's value with the print() function.
When a for loop iterates over a list, each iteration assigns the current element to the variable element. Therefore, any desired operations can be performed on element within the loop body, such as calculations, conditional checks, and so on.
Note that when iterating over a list with a for loop, the loop variable element takes on each element of the list in turn, but the list elements themselves cannot be modified directly. If you need to change the value of an element in the list, you can do so by index, for example my_list[[index]] <- new_value。
The output of the code above is:
[1] 1
[1] 2
[1] 3
[1] "a"
[1] "b"
[1] "c"
Combining lists
We can use the c() function to combine multiple lists into one:
Example
Create two lists
list1
<-
list
(
1
,
2
,
3
)
list2
<-
list
(
"Google"
,
"Runoob"
,
"Taobao"
)
Combining lists
merged.
list
<-
c
(
list1,list2
)
Display the combined list
(
merged.
list
)
The output of the code above is:
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] "Google"
[[5]]
[1] "Runoob"
[[6]]
[1] "Taobao"
Converting a list to a vector
To convert a list to a vector, you can use the unlist() function. Converting a list to a vector makes it easier to perform arithmetic operations:
Example
Creating a list
list1
<-
list
(
1
:
5
)
(
list1
)
list2
<-
list
(
10
:
14
)
(
list2
)
Convert to a vector
v1
<-
unlist
(
list1
)
v2
<-
unlist
(
list2
)
(
v1
)
(
v2
)
Add two vectors
result
<-
v1
v2
(
result
)
The output of the code above is:
[[1]]
[1] 1 2 3 4 5
[[1]]
[1] 10 11 12 13 14
[1] 1 2 3 4 5
[1] 10 11 12 13 14
[1] 11 13 15 17 19
Summary
The following are some commonly used R list operations and functions:
Creating a list:
- Using the c() function: for example, list1 <- c(1, 2, 3) creates a list containing 1, 2, and 3.
- Using the list() function: for example, list2 <- list(1, "a", TRUE) creates a list containing elements of different types.
Accessing list elements:
- Using an index: Access elements of a list by index. For example, list1[1] returns the first element of the list.
- Using element names: If the elements of a list have names, you can access them by name. For example, list3 <- list(a = 1, b = 2) allows elements to be accessed via list3$a and list3$b.
List operations:
- Length: Use the length() function to get the length of a list. For example, length(list1) returns the length of list1.
- Combine: Use the c() function or the append() function to combine two or more lists into one. For example, list4 <- c(list1, list2) combines list1 and list2.
- Add elements: Use the c() function to add elements to an existing list. For example, list1 <- c(list1, 4) adds 4 to the end of list1.
- Delete elements: Use an index and the negative index operator - to remove elements from a list. For example, list1 <- list1[-2] deletes the second element of list1.
List iteration:
- for loop: Use a for loop to iterate over the elements of a list. For example, for (element in list1) { ... } iterates over each element in list1.
- lapply() function: Apply a function to each element of a list and return a list of results. For example, new_list <- lapply(list1, function(x) x * 2) multiplies each element of list1 by 2.