知方号

知方号

R语言 列表

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 hereempId = 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 vectorempName = 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 commandempList = 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 componentsempId = c(1, 2, 3, 4)empName = c("Debi", "Sandeep", "Subham", "Shiba")numberOfEmp = 4empList = list(  "ID" = empId,  "Names" = empName,  "Total Staff" = numberOfEmp  )print(empList) # Accessing components by namescat("Accessing name components using command ")print(empListNames)

输出

$ID[1] 1 2 3 4$Names[1] "Debi" "Sandeep" "Subham" "Shiba" $`Total Staff`[1] 4Accessing name components using $ command[1] "Debi" "Sandeep" "Subham" "Shiba" 通过索引访问组件: 我们也可以使用索引来访问列表的组件。要访问列表的顶层组件,我们必须使用双切分运算符”[ []]” ,也就是两个方括号,如果我们想访问列表的低层或内层组件,我们必须使用另一个方括号 “[]” 和双切分运算符”[[ ] “。

例子

# R program to access# components of a list # Creating a list by naming all its componentsempId = c(1, 2, 3, 4)empName = c("Debi", "Sandeep", "Subham", "Shiba")numberOfEmp = 4empList = list(  "ID" = empId,  "Names" = empName,  "Total Staff" = numberOfEmp  )print(empList) # Accessing a top level components by indicescat("Accessing name components using indices ")print(empList[[2]]) # Accessing a inner level components by indicescat("Accessing Sandeep from name using indices ")print(empList[[2]][2]) # Accessing another inner level components by indicescat("Accessing 4 from ID using indices ")print(empList[[1]][4])

输出

$ID[1] 1 2 3 4$Names[1] "Debi" "Sandeep" "Subham" "Shiba" $`Total Staff`[1] 4Accessing 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 componentsempId = c(1, 2, 3, 4)empName = c("Debi", "Sandeep", "Subham", "Shiba")numberOfEmp = 4empList = list(  "ID" = empId,  "Names" = empName,  "Total Staff" = numberOfEmp)cat("Before modifying the list ")print(empList) # Modifying the top-level componentempList$`Total Staff` = 5 # Modifying inner level componentempList[[1]][5] = 5empList[[2]][5] = "Kamala" cat("After modified the list ")print(empList)

输出

Before modifying the listID[1] 1 2 3 4Names[1] "Debi" "Sandeep" "Subham" "Shiba" `Total Staff`[1] 4After modified the listID[1] 1 2 3 4 5Names[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 componentsempId = c(1, 2, 3, 4)empName = c("Debi", "Sandeep", "Subham", "Shiba")numberOfEmp = 4empList = list(  "ID" = empId,  "Names" = empName,  "Total Staff" = numberOfEmp)cat("Before concatenation of the new list ")print(empList) # Creating another listempAge = c(34, 23, 18, 45)empAgeList = list(  "Age" = empAge) # Concatenation of list using concatenation operatorempList = c(empList, empAgeList) cat("After concatenation of the new list ")print(empList)

输出

Before concatenation of the new listID[1] 1 2 3 4Names[1] "Debi" "Sandeep" "Subham" "Shiba" `Total Staff`[1] 4After concatenation of the new listID[1] 1 2 3 4Names[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 componentsempId = c(1, 2, 3, 4)empName = c("Debi", "Sandeep", "Subham", "Shiba")numberOfEmp = 4empList = list(  "ID" = empId,  "Names" = empName,  "Total Staff" = numberOfEmp)cat("Before deletion the list is ")print(empList) # Deleting a top level componentscat("After Deleting Total staff components ")print(empList[-3]) # Deleting a inner level componentscat("After Deleting sandeep from name ")print(empList[[2]][-2])

输出

Before deletion the list isID[1] 1 2 3 4Names[1] "Debi" "Sandeep" "Subham" "Shiba" `Total Staff`[1] 4After 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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lizi9903@foxmail.com举报,一经查实,本站将立刻删除。