R list
A list in R is a collection of objects that can store different types of data, such as numbers, strings, vectors, another list, and even matrices and functions.
To create a list in R, use the list()
function.
Here is an example where we create a list containing strings, vectors, and numbers:
Example
list_data <- list("tutorialpro", "google", c(11,22,33), 123, 51.23, 119.1)
print(list_data)
Executing the above code produces the following output:
[[1]]
[1] "tutorialpro"
[[2]]
[1] "google"
[[3]]
[1] 11 22 33
[[4]]
[1] 123
[[5]]
[1] 51.23
[[6]]
[1] 119.1
We can use the names()
function to name the elements of the list:
Example
# List containing vector, matrix, list
list_data <- list(c("Google","tutorialpro","Taobao"), matrix(c(1,2,3,4,5,6), nrow = 2),
list("tutorialpro",12.3))
# Set names for 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" "tutorialpro" "Taobao"
$Numbers
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
$Lists
$Lists[[1]]
[1] "tutorialpro"
$Lists[[2]]
[1] 12.3
Accessing List Elements
Elements in a list can be accessed using their index. If the elements are named using the names()
function, we can also access them by their names:
Example
# List containing vector, matrix, list
list_data <- list(c("Google","tutorialpro","Taobao"), matrix(c(1,2,3,4,5,6), nrow = 2),
list("tutorialpro",12.3))
# Set names for list elements
names(list_data) <- c("Sites", "Numbers", "Lists")
# Display the first element of 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" "tutorialpro" "Taobao"
$Lists
$Lists[[1]]
[1] "tutorialpro"
$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 in a list as shown in the following example:
Example
# List containing vector, matrix, list
list_data <- list(c("Google","tutorialpro","Taobao"), matrix(c(1,2,3,4,5,6), nrow = 2),
list("tutorialpro",12.3))
# Set names for 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
# 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] "New Element"
$<NA>
NULL
$Lists
[1] "I replaced the third element"
Merging Lists
We can merge multiple lists into one using the c()
function:
Example
# Create two lists
list1 <- list(1,2,3)
list2 <- list("Google","tutorialpro","Taobao")
# Merge 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] "tutorialpro"
[[6]]
[1] "Taobao"
[1] "tutorialpro"
[[6]] [1] "Taobao"
Convert List to Vector
To convert a list to a vector, you can use the unlist()
function. Converting a list to a vector can facilitate arithmetic operations:
Example
# Create a list
list1 <- list(1:5)
print(list1)
list2 <- list(10:14)
print(list2)
# Convert to vector
v1 <- unlist(list1)
v2 <- unlist(list2)
print(v1)
print(v2)
# Add two vectors
result <- v1 + v2
print(result)
Executing the above code will produce 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