R Lists
A list is a collection of objects in R language, which can be used to store different types of data, including numbers, strings, vectors, another list, matrix, data frame, etc., and of course it can also contain matrices and functions.
A list is a flexible data structure that can store and manipulate various types of data objects.
Creating Lists
To create a list in R language, use thelist()function.
In the following example, we create a list containing strings, vectors, and numbers:
Example
print(list_data)
Executing the above code produces the following output:
[[1]] [1] "example" [[2]] [1] "google" [[3]] [1] 11 22 33 [[4]] [1] 123 [[5]] [1] 51.23 [[6]] [1] 119.1
We can also use thec()function to create lists, and we can also use this function to merge multiple objects into one list, for example:
my_list <- c(object1, object2, object3)
Example
numbers <- c(1, 2, 3, 4, 5)
# Create a vector containing characters
characters <- c("apple", "banana", "orange")
# Merge two numeric vectors
merged_vector <- c(numbers, c(6, 7, 8))
# Merge two character vectors
merged_characters <- c(characters, c("grape", "melon"))
We can use thenames()function to name the elements of a list:
Example
list_data <- list(c("Google","Example","Taobao"), matrix(c(1,2,3,4,5,6), nrow = 2),
list("example",12.3))
# Set names for the list elements
names(list_data) <- c("Sites", "Numbers", "Lists")
# Display the list
print(list_data)
Executing the above code produces the following output:
$Sites
[1] "Google" "Example" "Taobao"
$Numbers
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
$Lists
$Lists[[1]]
[1] "example"
$Lists[[2]]
[1] 12.3
Accessing Lists
Elements in a list can be accessed using indices. If you use thenames()function to name them, we can also access them using the corresponding names:
Example
list_data <- list(c("Google","Example","Taobao"), matrix(c(1,2,3,4,5,6), nrow = 2),
list("example",12.3))
# Set names for the list elements
names(list_data) <- c("Sites", "Numbers", "Lists")
# Display the list
print(list_data[1])
# Access the third element of the list
print(list_data[3])
# Access the first vector element
print(list_data$Numbers)
Executing the above code produces the following output:
$Sites
[1] "Google" "Example" "Taobao"
$Lists
$Lists[[1]]
[1] "example"
$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 elements of a list, as shown in the following example:
Example
list_data <- list(c("Google","Example","Taobao"), matrix(c(1,2,3,4,5,6), nrow = 2),
list("example",12.3))
# Set names for the list elements
names(list_data) <- c("Sites", "Numbers", "Lists")
# Add an element
list_data[4] <- "New element"
print(list_data[4])
# Delete an element
list_data[4] <- NULL
# The output after deletion is NULL
print(list_data[4])
# Update an element
list_data[3] <- "I replaced the third element"
print(list_data[3])
Executing the above code produces the following output:
[[1]] [1] "新元素" $<NA> NULL $Lists [1] "我替换来第三个元素"
Using theforloop to iterate over a list:
Example
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) {
print(element)
}
In the above code, the for loop iterates through each element in the list my_list in sequence and stores each element in the variable element. Then, we can perform specific operations on each element within the loop body, such as using the print() function to print the value of the element.
When iterating over a list with a for loop, each iteration assigns the current element to the variable element. Therefore, within the loop body, you can perform any required operation on element, such as calculations, conditional checks, etc.
Note that when iterating over a list with a for loop, the loop variable element will take on each element in the list in turn, but it cannot directly modify the list element itself. If you need to modify the value of an element in the list, you can do so by indexing, for examplemy_list[[index]] <- new_value。
Executing the above code produces the following output:
[1] 1 [1] 2 [1] 3 [1] "a" [1] "b" [1] "c"
Merging Lists
We can use thec()function to merge multiple lists into one list:
Example
list1 <- list(1,2,3)
list2 <- list("Google","Example","Taobao")
# Merge the lists
merged.list <- c(list1,list2)
# Display the merged list
print(merged.list)
Executing the above code produces the following output:
[[1]] [1] 1 [[2]] [1] 2 [[3]] [1] 3 [[4]] [1] "Google" [[5]] [1] "Example" [[6]] [1] "Taobao"
Converting Lists to Vectors
To convert a list to a vector, you can use theunlist()function. Converting a list to a vector makes it convenient for us to perform arithmetic operations:
Example
list1 <- list(1:5)
print(list1)
list2 <-list(10:14)
print(list2)
# Convert to a vector
v1 <- unlist(list1)
v2 <- unlist(list2)
print(v1)
print(v2)
# Add two vectors
result <- v1+v2
print(result)
Executing the above code produces the following output:
[[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 language list operations and functions:
Creating lists:
- Using thec()function: for example, list1 <- c(1, 2, 3) creates a list containing 1, 2, and 3.
- Using thelist() function: for example, list2 <- list(1, "a", TRUE) creates a list containing elements of different types.
Accessing list elements:
- Using indices: access the elements in a list by index. For example, list1
- Using element names: if the elements in a list have names, you can use the names to access them. For example, with list3 <- list(a = 1, b = 2), you can access the elements 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 the list list1.
- Merging: use the c() function or the append() function to merge two or more lists into one list. For example, list4 <- c(list1, list2) merges the lists list1 and list2.
- Adding elements: use the c() function to add an element to an existing list. For example, list1 <- c(list1, 4) adds 4 to the end of the list list1.
- Deleting elements: use indices and the negative index operator - to delete elements from a list. For example, list1 <- list1[-2] deletes the second element in the list list1.
List iteration:
- for loop: use a for loop to iterate over the elements in a list. For example,for (element in list1) { ... }iterates through each element in the list list1.
- lapply()lapply() function: applies a function to each element in a list and returns a result list. For example, new_list <- lapply(list1, function(x) x * 2) multiplies each element in the list list1 by 2.