课程表

R语言 基础教程

R语言 图表

R语言 数据接口

R语言 统计示例

工具箱
速查手册

R语言 列表

当前位置:免费教程 » 程序设计 » R语言

列表是R语言对象,它包含不同类型的元素,如数字,字符串,向量和其中的另一个列表。 列表还可以包含矩阵或函数作为其元素。 列表是使用list()函数创建的。

创建列表

以下是创建包含字符串,数字,向量和逻辑值的列表的示例

  1. # Create a list containing strings, numbers, vectors and a logical values.
  2. list_data <- list("Red", "Green", c(21,32,11), TRUE, 51.23, 119.1)
  3. print(list_data)

当我们执行上面的代码,它产生以下结果 -

  1. [[1]]
  2. [1] "Red"
  3.  
  4. [[2]]
  5. [1] "Green"
  6.  
  7. [[3]]
  8. [1] 21 32 11
  9.  
  10. [[4]]
  11. [1] TRUE
  12.  
  13. [[5]]
  14. [1] 51.23
  15.  
  16. [[6]]
  17. [1] 119.1

命名列表元素

列表元素可以给出名称,并且可以使用这些名称访问它们。

  1. # Create a list containing a vector, a matrix and a list.
  2. list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2),
  3. list("green",12.3))
  4.  
  5. # Give names to the elements in the list.
  6. names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")
  7.  
  8. # Show the list.
  9. print(list_data)

当我们执行上面的代码,它产生以下结果 -

  1. $`1st_Quarter`
  2. [1] "Jan" "Feb" "Mar"
  3.  
  4. $A_Matrix
  5. [,1] [,2] [,3]
  6. [1,] 3 5 -2
  7. [2,] 9 1 8
  8.  
  9. $A_Inner_list
  10. $A_Inner_list[[1]]
  11. [1] "green"
  12.  
  13. $A_Inner_list[[2]]
  14. [1] 12.3

访问列表元素

列表的元素可以通过列表中元素的索引访问。 在命名列表的情况下,它也可以使用名称来访问。

我们继续使用在上面的例子列表 -

  1. # Create a list containing a vector, a matrix and a list.
  2. list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2),
  3. list("green",12.3))
  4.  
  5. # Give names to the elements in the list.
  6. names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")
  7.  
  8. # Access the first element of the list.
  9. print(list_data[1])
  10.  
  11. # Access the thrid element. As it is also a list, all its elements will be printed.
  12. print(list_data[3])
  13.  
  14. # Access the list element using the name of the element.
  15. print(list_data$A_Matrix)

当我们执行上面的代码,它产生以下结果 -

  1. $`1st_Quarter`
  2. [1] "Jan" "Feb" "Mar"
  3.  
  4. $A_Inner_list
  5. $A_Inner_list[[1]]
  6. [1] "green"
  7.  
  8. $A_Inner_list[[2]]
  9. [1] 12.3
  10.  
  11. [,1] [,2] [,3]
  12. [1,] 3 5 -2
  13. [2,] 9 1 8

操控列表元素

我们可以添加,删除和更新列表元素,如下所示。 我们只能在列表的末尾添加和删除元素。 但我们可以更新任何元素。

  1. # Create a list containing a vector, a matrix and a list.
  2. list_data <- list(c("Jan","Feb","Mar"), matrix(c(3,9,5,1,-2,8), nrow = 2),
  3. list("green",12.3))
  4.  
  5. # Give names to the elements in the list.
  6. names(list_data) <- c("1st Quarter", "A_Matrix", "A Inner list")
  7.  
  8. # Add element at the end of the list.
  9. list_data[4] <- "New element"
  10. print(list_data[4])
  11.  
  12. # Remove the last element.
  13. list_data[4] <- NULL
  14.  
  15. # Print the 4th Element.
  16. print(list_data[4])
  17.  
  18. # Update the 3rd Element.
  19. list_data[3] <- "updated element"
  20. print(list_data[3])

当我们执行上面的代码,它产生以下结果 -

  1. [[1]]
  2. [1] "New element"
  3.  
  4. $
  5. NULL
  6.  
  7. $`A Inner list`
  8. [1] "updated element"

合并列表

通过将所有列表放在一个list()函数中,您可以将许多列表合并到一个列表中。

  1. # Create two lists.
  2. list1 <- list(1,2,3)
  3. list2 <- list("Sun","Mon","Tue")
  4.  
  5. # Merge the two lists.
  6. merged.list <- c(list1,list2)
  7.  
  8. # Print the merged list.
  9. print(merged.list)

当我们执行上面的代码,它产生以下结果 -

  1. [[1]]
  2. [1] 1
  3.  
  4. [[2]]
  5. [1] 2
  6.  
  7. [[3]]
  8. [1] 3
  9.  
  10. [[4]]
  11. [1] "Sun"
  12.  
  13. [[5]]
  14. [1] "Mon"
  15.  
  16. [[6]]
  17. [1] "Tue"

将列表转换为向量

列表可以转换为向量,使得向量的元素可以用于进一步的操作。 可以在将列表转换为向量之后应用对向量的所有算术运算。 要做这个转换,我们使用unlist()函数。 它将列表作为输入并生成向量。

  1. # Create lists.
  2. list1 <- list(1:5)
  3. print(list1)
  4.  
  5. list2 <-list(10:14)
  6. print(list2)
  7.  
  8. # Convert the lists to vectors.
  9. v1 <- unlist(list1)
  10. v2 <- unlist(list2)
  11.  
  12. print(v1)
  13. print(v2)
  14.  
  15. # Now add the vectors
  16. result <- v1+v2
  17. print(result)

当我们执行上面的代码,它产生以下结果 -

  1. [[1]]
  2. [1] 1 2 3 4 5
  3.  
  4. [[1]]
  5. [1] 10 11 12 13 14
  6.  
  7. [1] 1 2 3 4 5
  8. [1] 10 11 12 13 14
  9. [1] 11 13 15 17 19
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号