R语言 列表
R中的列表是一个由有序的对象集合组成的通用对象。列表是一维的、异质的数据结构。列表可以是一个向量的列表,一个矩阵的列表,一个字符的列表和一个函数的列表,等等。
列表是一个向量,但有异质的数据元素。R中的列表是通过使用 list() 函数来创建的。R允许通过使用索引值来访问列表中的元素。在 R 中,列表的索引从 1 开始,而不是像其他编程语言那样从 0 开始。
创建一个列表
要在R中创建一个列表,你需要使用名为 “list() “的函数。换句话说,一个列表是一个包含其他对象的通用向量。为了说明列表的样子,我们在这里举一个例子。我们想建立一个带有细节的雇员列表。因此,我们希望得到诸如ID、雇员姓名和雇员人数等属性。
例子
# R program to create a List
# The first attributes is a numeric vector
# containing the employee IDs which is created
# using the command here
empId = c(1, 2, 3, 4)
# The second attribute is the employee name
# which is created using this line of code here
# which is the character vector
empName = c("Debi", "Sandeep", "Subham", "Shiba")
# The third attribute is the number of employees
# which is a single numeric variable.
numberOfEmp = 4
# We can combine all these three different
# data types into a list
# containing the details of employees
# which can be done using a list command
empList = list(empId, empName, numberOfEmp)
print(empList)
输出
[[1]]
[1] 1 2 3 4
[[2]]
[1] "Debi" "Sandeep" "Subham" "Shiba"
[[3]]
[1] 4
访问列表中的组件
我们可以通过两种方式访问列表中的组件。
- 通过名字访问组件: 一个列表中的所有组件都可以被命名,我们可以用这些名字来访问列表中的组件,使用 dollar 命令。
例子
# R program to access
# components of a list
# Creating a list by naming all its components
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)
print(empList)
# Accessing components by names
cat("Accessing name components using command\n")
print(empListNames)
输出
$ID
[1] 1 2 3 4
$Names
[1] "Debi" "Sandeep" "Subham" "Shiba"
$`Total Staff`
[1] 4
Accessing name components using $ command
[1] "Debi" "Sandeep" "Subham" "Shiba"
- 通过索引访问组件: 我们也可以使用索引来访问列表的组件。要访问列表的顶层组件,我们必须使用双切分运算符”[ []]” ,也就是两个方括号,如果我们想访问列表的低层或内层组件,我们必须使用另一个方括号 “[]” 和双切分运算符”[[ ] “。
例子
# R program to access
# components of a list
# Creating a list by naming all its components
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)
print(empList)
# Accessing a top level components by indices
cat("Accessing name components using indices\n")
print(empList[[2]])
# Accessing a inner level components by indices
cat("Accessing Sandeep from name using indices\n")
print(empList[[2]][2])
# Accessing another inner level components by indices
cat("Accessing 4 from ID using indices\n")
print(empList[[1]][4])
输出
$ID
[1] 1 2 3 4
$Names
[1] "Debi" "Sandeep" "Subham" "Shiba"
$`Total Staff`
[1] 4
Accessing name components using indices
[1] "Debi" "Sandeep" "Subham" "Shiba"
Accessing Sandeep from name using indices
[1] "Sandeep"
Accessing 4 from ID using indices
[1] 4
修改列表中的组件
列表也可以通过访问组件并将其替换为你想要的组件来进行修改。
例子
# R program to edit
# components of a list
# Creating a list by naming all its components
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)
cat("Before modifying the list\n")
print(empList)
# Modifying the top-level component
empList$`Total Staff` = 5
# Modifying inner level component
empList[[1]][5] = 5
empList[[2]][5] = "Kamala"
cat("After modified the list\n")
print(empList)
输出
Before modifying the list
ID
[1] 1 2 3 4Names
[1] "Debi" "Sandeep" "Subham" "Shiba"
`Total Staff`
[1] 4
After modified the listID
[1] 1 2 3 4 5
Names
[1] "Debi" "Sandeep" "Subham" "Shiba" "Kamala"`Total Staff`
[1] 5
列表的连接
两个列表可以使用连接函数进行连接。因此,当我们要连接两个列表时,必须使用连接运算符。
语法
list = c(list, list1)
list = 原始列表
list1 = 新列表
例子
# R program to edit
# components of a list
# Creating a list by naming all its components
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)
cat("Before concatenation of the new list\n")
print(empList)
# Creating another list
empAge = c(34, 23, 18, 45)
empAgeList = list(
"Age" = empAge
)
# Concatenation of list using concatenation operator
empList = c(empList, empAgeList)
cat("After concatenation of the new list\n")
print(empList)
输出
Before concatenation of the new list
ID
[1] 1 2 3 4Names
[1] "Debi" "Sandeep" "Subham" "Shiba"
`Total Staff`
[1] 4
After concatenation of the new listID
[1] 1 2 3 4
Names
[1] "Debi" "Sandeep" "Subham" "Shiba"`Total Staff`
[1] 4
$Age
[1] 34 23 18 45
删除列表中的组件
要删除列表中的组件,首先,我们需要访问这些组件,然后在这些组件前插入一个负号。它表示我们必须删除该组件。
例子
# R program to access
# components of a list
# Creating a list by naming all its components
empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)
cat("Before deletion the list is\n")
print(empList)
# Deleting a top level components
cat("After Deleting Total staff components\n")
print(empList[-3])
# Deleting a inner level components
cat("After Deleting sandeep from name\n")
print(empList[[2]][-2])
输出
Before deletion the list is
ID
[1] 1 2 3 4Names
[1] "Debi" "Sandeep" "Subham" "Shiba"
`Total Staff`
[1] 4
After Deleting Total staff componentsID
[1] 1 2 3 4
$Names
[1] "Debi" "Sandeep" "Subham" "Shiba"
After Deleting sandeep from name
[1] "Debi" "Subham" "Shiba"
合并列表
我们可以通过将所有的列表放入一个单一的列表来合并列表。
# Create two lists.
lst1 <- list(1,2,3)
lst2 <- list("Sun","Mon","Tue")
# Merge the two lists.
new_list <- c(lst1,lst2)
# Print the merged list.
print(new_list)
输出
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] "Sun"
[[5]]
[1] "Mon"
[[6]]
[1] "Tue"
将列表转换为矢量
在这里,我们将把列表转换为矢量,为此,我们将首先创建一个列表,然后将列表解列为矢量。
# Create lists.
lst <- list(1:5)
print(lst)
# Convert the lists to vectors.
vec <- unlist(lst)
print(vec)
输出
[[1]]
[1] 1 2 3 4 5
[1] 1 2 3 4 5
R列表到矩阵
我们将使用R编程中的matrix()函数创建矩阵。另一个将被使用的函数是unlist()函数,将列表转换为矢量。
# Defining list
lst1 <- list(list(1, 2, 3),
list(4, 5, 6))
# Print list
cat("The list is:\n")
print(lst1)
cat("Class:", class(lst1), "\n")
# Convert list to matrix
mat <- matrix(unlist(lst1), nrow = 2, byrow = TRUE)
# Print matrix
cat("\nAfter conversion to matrix:\n")
print(mat)
cat("Class:", class(mat), "\n")
输出
The list is:
[[1]]
[[1]][[1]]
[1] 1
[[1]][[2]]
[1] 2
[[1]][[3]]
[1] 3
[[2]]
[[2]][[1]]
[1] 4
[[2]][[2]]
[1] 5
[[2]][[3]]
[1] 6
Class: list
After conversion to matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
Class: matrix