📜  R编程中的列表操作

📅  最后修改于: 2022-05-13 01:55:12.600000             🧑  作者: Mango

R编程中的列表操作

R 语言中的列表是包含各种类型元素的对象,如数字、字符串、逻辑值、向量、列表中的列表以及作为其元素的矩阵和函数。

使用list()函数生成一个列表。它基本上是一个包含不同对象的通用向量。 R 允许其用户对列表执行各种操作,这些操作可用于以不同形式说明数据。

创建列表

R 中的列表可以通过将序列放在list()函数中来创建。

R
# Creating a list.
Geek_list <- list("Geek", "RList”, c(65, 21, 80), TRUE, 27.02, 10.3)
print(Geek_list)


R
# Creating a List
Geek_list <- list(c("Geeks", "For", "Geeks"), 
                    matrix(c(1:9), nrow = 3), 
                          list("Geek", 12.3))
   
# Naming each element of the list
names(Geek_list) <- c("This_is_a_vector", 
                      "This_is_a_Matrix", 
                      "This_is_a_listwithin_the_list")
   
# Printing the list
print(Geek_list)


R
# Creating a List
Geek_list <- list(c("Geeks", "For", "Geeks"), 
                    matrix(c(1:9), nrow = 3), 
                          list("Geek", 12.3))
   
# Naming each element of the list
names(Geek_list) <- c("This_is_a_vector", 
                      "This_is_a_Matrix", 
                      "This_is_a_listwithin_the_list")
  
# To access the first element of the list.      
print(Geek_list[1])
    
#  To access the third element.      
print(Geek_list[3])
     
# To access the list element using the name of the element.      
print(Geek_list$This_is_a_Matrix)


R
# Creating a List
Geek_list <- list(c("Geeks", "For", "Geeks"), 
                    matrix(c(1:9), nrow = 3), 
                          list("Geek", 12.3))
   
# Naming each element of the list
names(Geek_list) <- c("This_is_a_vector", 
                      "This_is_a_Matrix", 
                      "This_is_a_listwithin_the_list")
  
#  To add a new element.
Geek_list[4] <- "New element"
print(Geek_list)
    
# To remove the last element.     
Geek_list[4] <- NULL
    
# To print the 4th Element.     
print(Geek_list[4])
     
# To update the 3rd Element.     
Geek_list[3] <- "updated element"
print(Geek_list[3])


R
# Firstly, create two lists.     
list1 <- list(1, 2, 3, 4, 5, 6, 7)
list2 <- list("Geeks", "For", "Geeks")
    
# Then to merge these two lists.     
merged_list <- c(list1, list2)
print(merged_list)


R
# Firstly, create lists.     
list1 <- list(1:5)
print(list1)
list2 <-list(11:15)
print(list2)
  
# Now,  convert the lists to vectors.      
v1 <- unlist(list1)
v2 <- unlist(list2)
print(v1)
print(v2)
    
# Now add the vectors    
result_vector <- v1+v2
print(result_vector)


输出:

[[1]]
[1] "Geek"

[[2]]
[1] "RList"

[[3]]
[1] 65 21 80

[[4]]
[1] TRUE

[[5]]
[1] 27.02

[[6]]
[1] 10.3

命名列表的元素

名称可以分配给列表的元素,并且这些名称可用于访问元素。

R

# Creating a List
Geek_list <- list(c("Geeks", "For", "Geeks"), 
                    matrix(c(1:9), nrow = 3), 
                          list("Geek", 12.3))
   
# Naming each element of the list
names(Geek_list) <- c("This_is_a_vector", 
                      "This_is_a_Matrix", 
                      "This_is_a_listwithin_the_list")
   
# Printing the list
print(Geek_list)

输出:

$This_is_a_vector
[1] "Geeks" "For"   "Geeks"

$This_is_a_Matrix
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

$This_is_a_listwithin_the_list
$This_is_a_listwithin_the_list[[1]]
[1] "Geek"

$This_is_a_listwithin_the_list[[2]]
[1] 12.3

访问列表的元素

为了访问列表元素,使用索引号,并且在命名列表的情况下,也可以使用其名称访问元素。

R

# Creating a List
Geek_list <- list(c("Geeks", "For", "Geeks"), 
                    matrix(c(1:9), nrow = 3), 
                          list("Geek", 12.3))
   
# Naming each element of the list
names(Geek_list) <- c("This_is_a_vector", 
                      "This_is_a_Matrix", 
                      "This_is_a_listwithin_the_list")
  
# To access the first element of the list.      
print(Geek_list[1])
    
#  To access the third element.      
print(Geek_list[3])
     
# To access the list element using the name of the element.      
print(Geek_list$This_is_a_Matrix)  

输出:

$This_is_a_vector
[1] "Geeks" "For"   "Geeks"

$This_is_a_listwithin_the_list
$This_is_a_listwithin_the_list[[1]]
[1] "Geek"

$This_is_a_listwithin_the_list[[2]]
[1] 12.3


     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

添加、删除和更新列表的元素

在 R 中,可以将新元素添加到列表中,可以删除或更新现有元素。

R

# Creating a List
Geek_list <- list(c("Geeks", "For", "Geeks"), 
                    matrix(c(1:9), nrow = 3), 
                          list("Geek", 12.3))
   
# Naming each element of the list
names(Geek_list) <- c("This_is_a_vector", 
                      "This_is_a_Matrix", 
                      "This_is_a_listwithin_the_list")
  
#  To add a new element.
Geek_list[4] <- "New element"
print(Geek_list)
    
# To remove the last element.     
Geek_list[4] <- NULL
    
# To print the 4th Element.     
print(Geek_list[4])
     
# To update the 3rd Element.     
Geek_list[3] <- "updated element"
print(Geek_list[3])

输出:

$This_is_a_vector
[1] "Geeks" "For"   "Geeks"

$This_is_a_Matrix
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

$This_is_a_listwithin_the_list
$This_is_a_listwithin_the_list[[1]]
[1] "Geek"

$This_is_a_listwithin_the_list[[2]]
[1] 12.3


[[4]]
[1] "New element"

$
NULL

$This_is_a_listwithin_the_list
[1] "updated element"

合并列表的元素

许多列表可以合并到一个列表中,通过该列表将所有列表元素放在一个列表中。

R

# Firstly, create two lists.     
list1 <- list(1, 2, 3, 4, 5, 6, 7)
list2 <- list("Geeks", "For", "Geeks")
    
# Then to merge these two lists.     
merged_list <- c(list1, list2)
print(merged_list)

输出:

[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 4

[[5]]
[1] 5

[[6]]
[1] 6

[[7]]
[1] 7

[[8]]
[1] "Geeks"

[[9]]
[1] "For"

[[10]]
[1] "Geeks"

将列表转换为向量

为了执行算术运算,应使用unlist()函数将列表转换为向量。

R

# Firstly, create lists.     
list1 <- list(1:5)
print(list1)
list2 <-list(11:15)
print(list2)
  
# Now,  convert the lists to vectors.      
v1 <- unlist(list1)
v2 <- unlist(list2)
print(v1)
print(v2)
    
# Now add the vectors    
result_vector <- v1+v2
print(result_vector)

输出:

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

[[1]]
[1] 11 12 13 14 15

[1] 1 2 3 4 5
[1] 11 12 13 14 15
[1] 12 14 16 18 20