在本教程中,您将学习 R 中的列表数据结构,包括如何创建列表、列表支持的函数等。
在 R 中,列表是一种通用向量的数据结构,包含异构数据类型的元素。简单来说,列表是一个包含不同数据类型元素的对象,如字符、数值、逻辑值、向量等。列表是一维的,是有序的不同(异构)数据类型元素的集合。
列表包含 基本数据类型 的集合,如数值、整数、字符类型元素,以形成一个异构数据结构。与其他编程语言一样,R 语言为构成列表的每个元素提供索引。在列表中,索引从 1 开始,就像 R 中的向量 一样,用于访问元素或进行某些列表元素操作。
在 R 中,使用 list() 函数创建列表。
Name_of_list = list(mention_list_elements)
考虑一个学生记录的例子,包括学生姓名、学号、班级、他们参加活动的项目。我们知道列表是一个通用的向量对象。换句话说,列表是向量的集合。
Student_Name = c("Alliet","James","Sarah")
Student_Rollno = c(3,11,19)
class = c("X") secti
item = c("Singing","Playing","Dance")
这里使用 c() 函数(链接到向量)创建了 5 个向量,分别是 Student_Name、Student_Rollno、class、section、item,它们是不同数据类型的元素。向量 Student_Name 是字符数据类型元素,向量 Student_Rollno 是数值数据类型元素,依此类推。
列表是一种数据结构,支持将所有这些创建的不同(异构)向量组织在一个结构下。student_list 是使用 list() 函数创建的。括号 () 中包含了构成列表的所有向量,如 Student_Name、Student_Rollno、class、section、item,如下所示
student_list = list(Student_Name,Student_Rollno,class,section,item) # created list using list()
现在让我们看看一起执行这些代码后产生的结果
Student_Name = c("Alliet","James","Sarah") #vectors are created of character, numeric data types
Student_Rollno = c(3,11,19)
class = c("X") secti
item = c("Singing","Playing","Dance")
student_list = list(Student_Name,Student_Rollno,class,section,item) #a list is created using list()
print(student_list) #prints the result
当代码执行时,它会显示一个向量列表 (student_list)。
输出
[[1]] [1] "Alliet" "James" "Sarah" [[2]] [1] 3 11 19 [[3]] [1] "X" [[4]] [1] "A" "B" "B" [[5]] [1] "Singing" "Playing" "Dance"

正如我们在上一篇教程中讨论的,列表也支持诸如 typeof()、length()、is.list() 等函数。
| 函数 | 描述 | R 代码 | 输出 |
|---|---|---|---|
| typeof() | 确定列表的数据类型 | typeof(student_list) |
> typeof(student_list) [1] "list" |
| length() | 检查列表长度 | length(student_list) |
> length(student_list) [1] 3 |
| is.list() | 检查列表长度 | is.list(student_list) |
> is.list(student_list) [1] TRUE> |
让我们看一个表格来比较向量和列表之间的区别。
| 向量 | 列表 (List) |
|---|---|
| 向量是同构数据结构。 | 列表是异构数据结构。 |
| 向量是一维的。 | 列表是多维的。 |
| 它是使用 c() 创建的。 | 它是使用 list() 创建的。 |
示例
|
示例
|
注意:在 R 编程中,向量和列表的索引都从 1 开始,这与 C、JAVA、python 等其他语言不同。
因此,列表是异构数据元素向量的集合。列表还可以包含除向量类型之外的数值、字符、逻辑等基本数据类型。在上面的例子中,我们讨论了由向量数据结构组成的列表。让我们创建一个包含数值、逻辑、整数和向量数据类型元素的列表。
a <- list(site = "Learn eTutorials", Lang ="R ", Year = 2022, tutorials =c("GO","PYTHON","DATA SCIENCE"))
输出
> a <- list(site = "Learn eTutorials",Lang ="R ", Year = 2022,tutorials =c("GO","PYTHON","DATA SCIENCE"))
> a
$site
[1] "Learn eTutorials"
$Lang
[1] "R "
$Year
[1] 2022
$tutorials
[1] "GO" "PYTHON" "DATA SCIENCE"
有两种方法/方式来创建列表
| 在单行中创建列表并声明变量 | 在多行中声明变量并创建列表 |
|---|---|
|
|
输出$A [1] 1 $B [1] "hello" "haii" $D [1] TRUE |
输出[[1]] [1] 1 [[2]] [1] "hello" "haii" [[3]] [1] TRUE |
| 输出与索引一起显示。[[1]] [[2]] [[3]] |
在 R 编程中,您可以通过两种方式为列表元素命名。
NEW_List = list ("name1" = <list _element>,"name2 "= ,….)
A = 1
B=c("hello","haii")
C =TRUE
NEW_List = list("first" = A,"Second"=B ,"Third"=C)
print(NEW_List)
print(NEW_List$Third)
NEW_List 是创建的列表,其名称分别为 "first"、"second"、"third",对应列表中的每个元素。
命名列表元素后对应的输出是
$first [1] 1 $Second [1] "hello" "haii" $Third [1] TRUE
让我们使用 names() 函数为上面讨论的程序创建一个名称。列表元素(向量)可以通过 c() 函数创建,该函数为列表元素的每个位置分配新的名称。
names(NEW_List)=c("name1","name2","name3")
让我们检查整个 R 源代码,以了解我们如何为列表元素命名
A = 1
B=c("hello","haii")
C =TRUE
# Creating a list containing a vector, a matrix and a list.
NEW_List = list(A,B ,C)
# Giving names to the elements in the list.
names(NEW_List)=c("first" ,"Second" ,"Third")
# Show the list.
print(NEW_List)
这里,第一个位置的 "first" 为 A = 1 命名,名称 "Second" 分配给 B=c("hello","haii"),依此类推。
命名列表元素后对应的输出是
$first [1] 1 $Second [1] "hello" "haii" $Third [1] TRUE
让我们看看两种方法之间的区别
| 在创建列表时命名 | 使用 names() 命名 |
|---|---|
|
语法:
|
语法:
|
|
输出 $first [1] 1 $Second [1] "hello" "haii" $Third [1] TRUE |
输出 $first [1] 1 $Second [1] "hello" "haii" $Third [1] TRUE |
您可以使用元素的索引或它们在列表中的名称来访问列表中的元素,即通过使用值的位置编号或元素名称。请看上面的表格示例和代码输出
A = 1
B=c("hello","haii")
D =TRUE
A = list(A,B,D)
print(A)
print(A[1])
print(A[2])
通过在方括号内提供索引号以及需要访问元素的列表名称,并在 print() 函数中,可以轻松访问元素。
> print(A[1]) [[1]] [1] 1 > print(A[2]) [[1]] [1] "hello" "haii"
$ 命令按名称访问列表元素
A = 1
B=c("hello","haii")
C =TRUE
NEW_List = list("first" = A,"Second"=B ,"Third"=C)
print(NEW_List)
# Access by names
cat("Access element by name using $ command\n")
print(NEW_List$Third)
输出
$first [1] 1 $Second [1] "hello" "haii" $Third [1] TRUE Access element by name using $ command [1] TRUE >
可以通过指定变量或向量类型的名称,并在方括号内使用下一个首选索引号,以及需要分配给它的值,来向列表添加新元素。
在前面的代码中,我们创建了一个索引值为 3 的列表。让我们向列表中添加一个新元素。
A[4]= "NEW ELEMENT"
print(A[4])
让我们看看上面的代码片段执行后,新元素的添加是如何进行的
A = 1
B=c("hello","haii")
D =TRUE
A = list(A,B,D)
print(A)
A[4]= "NEW ELEMENT"
print(A[4])
我们在索引 4 处添加了一个新元素。让我们看看添加新元素后列表 A 发生的变化。使用 print(A) 函数显示或查看整个列表。
[[1]] [1] 1 [[2]] [1] "hello" "haii" [[3]] [1] TRUE [[1]] [1] "NEW ELEMENT"
在 R 中,通过在需要从列表中移除/删除的元素的索引上指示负号来移除列表元素。
print(Variable_name[-<index>])
Student_Name = c("Alliet","James","Sarah")
Student_Rollno = c(3,11,19)
class = c("X") secti
item = c("Singing","Playing","Dance")
student_list = list(Student_Name,Student_Rollno,class)
print(student_list)
print(student_list[-1])
当代码 print(student_list[-1]) 执行时,它会导致移除索引 1 处的值。该列表将只包含元素 Student_Rollno 和 class。
输出
> print(student_list) [[1]] [1] "Alliet" "James" "Sarah" [[2]] [1] 3 11 19 [[3]] [1] "X" > print(student_list[-1]) [[1]] [1] 3 11 19 [[2]] [1] "X" >
您可以通过指定列表中需要修改或更新的元素的索引来轻松更新或修改列表元素值。
这些代码通过提供索引值来更新元素或值。
student_list[[2]][2]= 14
#updated with a new value at second position of the 2nd index element in list
student_list[[1]][3]="Williams" #added element at third position of first index element in list
student_list[[1]][3]="Stephen" #updated with new name
print(student_list)
当这些代码执行时 student_list[[2]][2]= 14
列表的第二个索引,即 Student_Rollno 被识别,并且在 Student_Rollno 中,索引 2 或第二个位置的元素从 11 更新为 14。需要更新或修改的值被赋给相应的索引位置。
[[1]] [1] "Alliet" "James" "Stephen" [[2]] [1] 3 14 19 [[3]] [1] "X"
您可以看到 Student_Name = c("Alliet","James”) 被更新为 "Alliet" "James" "Stephen"。同样地,Student_Rollno = c(3,11,19) 被修改为 3 14 19。
str() 函数是什么?或者如何检查列表的结构?在 R 中,使用 str() 函数显示列表的内部结构。
我们之前示例中的 student_list 的内部结构是使用 str() 函数检查的。命令 str(student_list) 显示列表结构如下
您可以从输出中推断出,str() 函数显示了列表元素的内容,包括索引范围及其基本数据类型。
可以使用以下语法在 R 列表中连接列表
New_list =c(list1,list2,…)
a =list("R",7,x=c(3,4))
b= list(y=c(TRUE,FALSE,FALSE),Z=34L)
NEW_LIST =c(a,b)
print(NEW_LIST)
输出
[[1]] [1] "R" [[2]] [1] 7 $x [1] 3 4 $y [1] TRUE FALSE FALSE $Z [1] 34 >
在 R 中,unlist() 函数用于将列表转换为向量。
unlist(list_name)
a =list("R",7,x=c(3,4)) #CREATED A LIST
b =unlist(a) #unlist converts list to vector
print(b)
输出
x1 x2
"R" "7" "3" "4"
| 函数 | 描述 |
|---|---|
| list() | 创建列表 |
| names() | 为列表附加名称 |
| typeof() | 确定列表的数据类型 |
| length() | 检查列表长度 |
| is.list() | 检查列表是否存在 |
| str() | 检查列表的内部结构 |
| unlist() | 将列表转换为向量。 |