📜  R – 列表

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

R – 列表

R 中的列表是由对象的有序集合组成的通用对象。列表是一维的异构数据结构。该列表可以是向量列表、矩阵列表、字符列表和函数列表等。

列表是一个向量,但具有异构数据元素。 R 中的列表是使用list()函数创建的。 R 允许使用索引值访问列表的元素。在 R 中,列表的索引从 1 开始,而不是像其他编程语言那样从 0 开始。

创建列表

要在 R 中创建列表,您需要使用名为“list()”的函数。换句话说,列表是包含其他对象的通用向量。为了说明列表的外观,我们在这里举个例子。我们想建立一个包含详细信息的员工列表。因此,为此,我们需要 ID、员工姓名和员工人数等属性。

例子:

R
# 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)


R
# 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(empList$Names)


R
# 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])


R
# 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)


R
# 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)


R
# 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])


R
# 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)


R
# Create lists.
lst <- list(1:5)
print(lst)
 
# Convert the lists to vectors.
vec <- unlist(lst)
 
print(vec)


R
# 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")


输出:

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

[[2]]
[1] "Debi"    "Sandeep" "Subham"  "Shiba"  

[[3]]
[1] 4

访问列表的组件

我们可以通过两种方式访问列表的组件。

  • 按名称访问组件:可以命名列表的所有组件,我们可以使用这些名称使用 $ 命令访问列表的组件。

例子:

R

# 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(empList$Names)

输出:

$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

# 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

# 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 4

$Names
[1] "Debi"    "Sandeep" "Subham"  "Shiba"  

$`Total Staff`
[1] 4

After modified the list
$ID
[1] 1 2 3 4 5

$Names
[1] "Debi"    "Sandeep" "Subham"  "Shiba"   "Kamala" 

$`Total Staff`
[1] 5

列表的串联

可以使用连接函数连接两个列表。因此,当我们想要连接两个列表时,我们必须使用连接运算符。

句法:

例子:

R

# 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 4

$Names
[1] "Debi"    "Sandeep" "Subham"  "Shiba"  

$`Total Staff`
[1] 4

After concatenation of the new list
$ID
[1] 1 2 3 4

$Names
[1] "Debi"    "Sandeep" "Subham"  "Shiba"  

$`Total Staff`
[1] 4

$Age
[1] 34 23 18 45

删除列表的组件

要删除列表的组件,首先,我们需要访问这些组件,然后在这些组件之前插入一个负号。它表明我们必须删除该组件。

例子:

R

# 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 4

$Names
[1] "Debi"    "Sandeep" "Subham"  "Shiba"  

$`Total Staff`
[1] 4

After Deleting Total staff components
$ID
[1] 1 2 3 4

$Names
[1] "Debi"    "Sandeep" "Subham"  "Shiba"  

After Deleting sandeep from name
[1] "Debi"   "Subham" "Shiba" 

合并列表

我们可以通过将所有列表放入一个列表来合并列表。

R

# 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"

将列表转换为向量

在这里,我们将列表转换为向量,为此我们将首先创建一个列表,然后将列表取消列表到向量中。

R

# 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()函数,用于将列表转换为向量。

R

# 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