English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

R Lists

Lists are a collection of objects in R language, which can be used to save different types of data, such as numbers, strings, vectors, another list, etc., of course, it can also contain matrices and functions.

R language creates lists using the list() function.

As shown in the following example, we create a list that contains strings, vectors, and numbers:

list_data <- list("w3codebox", "google", c(11,22,33), 123, 51.23, 119.1)
print(list_data)

The output of the above code is:

[[1]]
[1] "w3codebox"
[[2]]
[1] "google"
[[3]]
[1] 11 22 33
[[4]]
[1] 123
[[5]]
[1] 51.23

We can use the names() function to name the elements of the list:

# List contains vectors, matrices, and lists
list_data <- list(c("Google","w3codebox","Taobao"), matrix(c(1,2,3,4,5,6), nrow = 2),
   list("w3codebox",12.3))
# Set names for list elements
names(list_data) <- c("Sites", "Numbers", "Lists")
# Display the list
print(list_data)

The output of the above code is:

$Sites
[1] "Google" "w3codebox" "Taobao"
$Numbers
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
$Lists
$Lists[[1]]
[1] "w3codebox"
$Lists[[2]]
[1] 12.3

Access the list

List elements can be accessed using indices, if using to names() After naming the function, we can also access it using the corresponding name:

# List contains vectors, matrices, and lists
list_data <- list(c("Google","w3codebox","Taobao"), matrix(c(1,2,3,4,5,6), nrow = 2),
   list("w3codebox",12.3))
# Set names for 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 element of the vector
print(list_data$Numbers)

The output of the above code is:

$Sites
[1] "Google" "w3codebox" "Taobao"
$Lists
$Lists[[1]]
[1] "w3codebox"
$Lists[[2]]
[1] 12.3
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

Operate list elements

We can perform operations such as adding, deleting, and updating lists, as shown in the following examples:

# List contains vectors, matrices, and lists
list_data <- list(c("Google","w3codebox","Taobao"), matrix(c(1,2,3,4,5,6), nrow = 2),
   list("w3codebox",12.3))
# Set names for list elements
names(list_data) <- c("Sites", "Numbers", "Lists")
# Add element
list_data[4] <- "New element"
print(list_data[4])
# Delete element
list_data[4] <- NULL
# Delete and output as NULL
print(list_data[4])
# Update element
list_data[3] <- "I replace the third element"
print(list_data[3])

The output of the above code is:

[[1]]
[1] "New element"
$<NA>
NULL
$Lists
[1] "I replace the third element"

Merge Lists

We can use the c() function to merge multiple lists into one list:

# Create two lists
list1 <- list(1,2,3)
list2 <- list("Google","w3codebox","Taobao")
# Merge lists
merged.list <- c(list(1,list2)
# Display the merged list
print(merged.list)

The output of the above code is:

[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] "Google"
[[5]]
[1] "w3codebox"
[[6]]
[1] "Taobao"

List to Vector

To convert a list to a vector, you can use the unlist() function to convert a list to a vector, which can facilitate arithmetic operations:

# 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)
# Two vector addition
result <- v1+v2
print(result)

The output of the above code 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