# R列表用法详解

## 本文概述

### 例子

``````vec <- c(3, 4, 5, 6)
char_vec<-c("shubham", "nishka", "gunjan", "sumit")
logic_vec<-c(TRUE, FALSE, FALSE, TRUE)
out_list<-list(vec, char_vec, logic_vec)
out_list``````

``````[[1]]
[1] 3 4 5 6
[[2]]
[1] "shubham" "nishka"  "gunjan"  "sumit"
[[3]]
[1]  TRUE FALSE FALSE  TRUE``````

## 列出创建

``list()``

``````list_1<-list(1, 2, 3)
list_2<-list("Shubham", "Arpita", "Vaishali")
list_3<-list(c(1, 2, 3))
list_4<-list(TRUE, FALSE, TRUE)
list_1
list_2
list_3
list_4``````

``````[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3

[[1]]
[1] "Shubham"
[[2]]
[1] "Arpita"
[[3]]
[1] "Vaishali"

[[1]]
[1] 1 2 3

[[1]]
[1] TRUE
[[2]]
[1] FALSE
[[3]]
[1] TRUE``````

``````list_data<-list("Shubham", "Arpita", c(1, 2, 3, 4, 5), TRUE, FALSE, 22.5, 12L)
print(list_data)``````

``````[[1]]
[1] "Shubham"
[[2]]
[1] "Arpita"
[[3]]
[1] 1 2 3 4 5
[[4]]
[1] TRUE
[[5]]
[1] FALSE
[[6]]
[1] 22.5
[[7]]
[1] 12``````

## 命名列出元素

R为访问元素提供了一种非常简单的方法, 即为列表的每个元素提供名称。通过为元素分配名称, 我们可以轻松访问元素。仅需三个步骤即可打印与名称相对应的列表数据：

1. 创建一个列表。
2. 借助names()函数为列表元素分配名称。
3. 打印列表数据。

``````# Creating a list containing a vector, a matrix and a list.
list_data <- list(c("Shubham", "Nishka", "Gunjan"), matrix(c(40, 80, 60, 70, 90, 80), nrow = 2), list("BCA", "MCA", "B.tech"))

# Giving names to the elements in the list.
names(list_data) <- c("Students", "Marks", "Course")

# Show the list.
print(list_data)``````

``````\$Students
[1] "Shubham" "Nishka"  "Gunjan"

\$Marks
[, 1] [, 2] [, 3]
[1, ]   40   60   90
[2, ]   80   70   80

\$Course
\$Course[[1]]
[1] "BCA"

\$Course[[2]]
[1] "MCA"

\$Course[[3]]
[1] "B. tech."``````

## 访问列表元素

R提供了两种访问列表元素的方式。第一个是以与向量相同的方式执行的索引方法。在第二篇文章中, 我们可以在名称的帮助下访问列表的元素。只有命名列表才有可能。如果列表正常, 则无法使用名称访问列表的元素。

``````# Creating a list containing a vector, a matrix and a list.
list_data <- list(c("Shubham", "Arpita", "Nishka"), matrix(c(40, 80, 60, 70, 90, 80), nrow = 2), list("BCA", "MCA", "B.tech"))
# Accessing the first element of the list.
print(list_data[1])

# Accessing the third element. The third element is also a list, so all its elements will be printed.
print(list_data[3])``````

``````[[1]]
[1] "Shubham" "Arpita"  "Nishka"

[[1]]
[[1]][[1]]
[1] "BCA"

[[1]][[2]]
[1] "MCA"

[[1]][[3]]
[1] "B.tech"``````

``````# Creating a list containing a vector, a matrix and a list.
list_data <- list(c("Shubham", "Arpita", "Nishka"), matrix(c(40, 80, 60, 70, 90, 80), nrow = 2), list("BCA", "MCA", "B.tech"))
# Giving names to the elements in the list.
names(list_data) <- c("Student", "Marks", "Course")
# Accessing the first element of the list.
print(list_data["Student"])
print(list_data\$Marks)
print(list_data)``````

``````\$Student
[1] "Shubham" "Arpita"  "Nishka"

[, 1] [, 2] [, 3]
[1, ]   40   60   90
[2, ]   80   70   80

\$Student
[1] "Shubham" "Arpita"  "Nishka"

\$Marks
[, 1] [, 2] [, 3]
[1, ]   40   60   90
[2, ]   80   70   80

\$Course
\$Course[[1]]
[1] "BCA"
\$Course[[2]]
[1] "MCA"
\$Course[[3]]
[1] "B. tech."``````

## 操纵列表元素

R允许我们添加, 删除或更新列表中的元素。我们可以从任何地方更新列表的元素, 但是元素只能在列表的末尾添加或删除。要从指定的索引中删除元素, 我们将为其分配一个空值。我们可以通过从新值覆盖列表元素来更新它。让我们看一个示例, 以了解如何添加, 删除或更新列表中的元素。

``````# Creating a list containing a vector, a matrix and a list.
list_data <- list(c("Shubham", "Arpita", "Nishka"), matrix(c(40, 80, 60, 70, 90, 80), nrow = 2), list("BCA", "MCA", "B.tech"))

# Giving names to the elements in the list.
names(list_data) <- c("Student", "Marks", "Course")

# Adding element at the end of the list.
print(list_data[4])

# Removing the last element.
list_data[4] <- NULL

# Printing the 4th Element.
print(list_data[4])

# Updating the 3rd Element.
list_data[3] <- "Masters of computer applications"
print(list_data[3])``````

``````[[1]]

\$<NA>
NULL

\$Course
[1] "Masters of computer applications"``````

## 将列表转换为矢量

unlist()函数将列表作为参数并更改为向量。让我们看一个示例, 以了解如何在R中使用unlist()函数。

``````# Creating lists.
list1 <- list(10:20)
print(list1)

list2 <-list(5:14)
print(list2)

# Converting the lists to vectors.
v1 <- unlist(list1)
v2 <- unlist(list2)

print(v1)
print(v2)

result <- v1+v2
print(result)``````

``````[[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``````

## 合并清单

R允许我们将一个或多个列表合并为一个列表。合并也借助list()函数完成。要合并列表, 我们必须将所有列表作为参数传递到list函数中, 然后它返回一个列表, 其中包含列表中存在的所有元素。让我们看一个示例, 以了解合并过程是如何完成的。

``````# Creating two lists.
Even_list <- list(2, 4, 6, 8, 10)
Odd_list <- list(1, 3, 5, 7, 9)

# Merging the two lists.
merged.list <- list(Even_list, Odd_list)

# Printing the merged list.
print(merged.list)``````

``````[[1]]
[[1]][[1]]
[1] 2

[[1]][[2]]
[1] 4

[[1]][[3]]
[1] 6

[[1]][[4]]
[1] 8

[[1]][[5]]
[1] 10

[[2]]
[[2]][[1]]
[1] 1

[[2]][[2]]
[1] 3

[[2]][[3]]
[1] 5

[[2]][[4]]
[1] 7

[[2]][[5]]
[1] 9``````

• 回顶