列表可以容纳各种类型的数据对象,向量,矩阵,数据框,甚至一个列表也可以成为另一个列表的元素。
1.创建
(example = list("abc", 3:5, matrix(1, nrow = 3, ncol = 4), data.frame(x = 1:4, y = paste0("boy_", 1:4))))
## [[1]]
## [1] "abc"
##
## [[2]]
## [1] 3 4 5
##
## [[3]]
## [,1] [,2] [,3] [,4]
## [1,] 1 1 1 1
## [2,] 1 1 1 1
## [3,] 1 1 1 1
##
## [[4]]
## x y
## 1 1 boy_1
## 2 2 boy_2
## 3 3 boy_3
## 4 4 boy_4
2.基本操作
(1)查看
str()函数可用来查看列表结构
(complex = list(first = list(1:2), second = list(letters, list(matrix(1:4, nrow = 2, ncol = 2)))))
## $first
## $first[[1]]
## [1] 1 2
##
##
## $second
## $second[[1]]
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
## [18] "r" "s" "t" "u" "v" "w" "x" "y" "z"
##
## $second[[2]]
## $second[[2]][[1]]
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
str(complex)
## List of 2
## $ first :List of 1
## ..$ : int [1:2] 1 2
## $ second:List of 2
## ..$ : chr [1:26] &