目录
1.创建列表
# 向量元素类型必须一致,按优先级自动转换
data1 <-c(66,'student',TRUE)
data1
## [1] "66" "student" "TRUE"
#列表元素类型可能不同
data2 <-list(66,'student',TRUE)
data2
## [[1]]
## [1] 66## [[2]]
## [1] "student"## [[3]]
## [1] TRUE
str():可以紧凑的显示对象内部结构
str(data2)
## List of 3
## $ : num 66
## $ : chr "student"
## $ : logi TRUE
a <- 1:5
b <- c('one', 'two', 'three')
c <- matrix(1:20, 4, 5)
mylist <- list(a, b, c)
mylist
## [[1]]
## [1] 1 2 3 4 5## [[2]]
## [1] "one" "two" "three"## [[3]]
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20
str(mylist)
## List of 3
## $ : int [1:5] 1 2 3 4 5
## $ : chr [1:3] "one" "two" "three"
## $ : int [1:4, 1:5] 1 2 3 4 5 6 7 8 9 10 ...
# 给列表元素添加名称
mylist <- list(first=a, second=b, third=c)
mylist
## $first
## [1] 1 2 3 4 5## $second
## [1] "one" "two" "three"## $third
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20
str(mylist)
## List of 3
## $ first : int [1:5] 1 2 3 4 5
## $ second: chr [1:3] "one" "two" "three"
## $ third : int [1:4, 1:5] 1 2 3 4 5 6 7 8 9 10 ...
2.访问列表
# 长度信息
length(mylist)
## [1] 3
# 名称信息
names(mylist)
## [1] "first" "second" "third"
# 返回值为列表
mylist[1]
## $first
## [1] 1 2 3 4 5
mylist[2]
## $second
## [1] "one" "two" "three"
mylist[3]
## $third
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20
class():查看数据结构
#class(mylist[1])
#class(mylist[2])
class(mylist[3])
## [1] "list"
mylist['first']
## [1] 1 2 3 4 5
mylist['second']
## $second
## [1] "one" "two" "three"
mylist['third']
## $third
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20
class(mylist['third'])
## [1] "list"
# 返回值为元素本身类型
mylist[[1]]
## [1] 1 2 3 4 5
mylist[[2]]
## [1] "one" "two" "three"
mylist[[3]]
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20
class(mylist[[1]])
## [1] "integer"
class(mylist[[2]])
## [1] "character"
matrix类型:矩阵类型
array类型:N维数组
class(mylist[[3]])
## [1] "matrix" "array"
# 返回值为元素本身类型
mylist[['first']]
## [1] 1 2 3 4 5
class(mylist[['first']])
## [1] "integer"
# 返回值为元素本身类型
mylist$first
## [1] 1 2 3 4 5
class(mylist$first)
## [1] "integer"
# 访问多个元素
# mylist(1, 3) #注意:写法有误
# mylist[1, 3] #注意:写法有误
# mylist[[1, 3]] #注意:写法有误
mylist[c(1, 3)]
## $first
## [1] 1 2 3 4 5## $third
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20
mylist[c('first', 'third')]
## $first
## [1] 1 2 3 4 5## $third
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20
mylist[c(TRUE, FALSE, TRUE)]
## $first
## [1] 1 2 3 4 5## $third
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20
3.修改列表
(1)增加元素
mylist[4] <- 'This is a list'
mylist['fourth'] <- 34
mylist$fifth <- FALSE
mylist
## $first
## [1] 1 2 3 4 5## $second
## [1] "one" "two" "three"## $third
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20## [[4]]
## [1] "This is a list"## $fourth
## [1] 34## $fifth
## [1] FALSE
(2)修改元素
mylist[4] <- 8888
mylist['fourth'] <- TRUE
mylist$fifth <- 'Student'
mylist
## $first
## [1] 1 2 3 4 5## $second
## [1] "one" "two" "three"## $third
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20## [[4]]
## [1] 8888## $fourth
## [1] TRUE## $fifth
## [1] "Student"
(3)删除元素
mylist <- mylist[-4]
mylist
## $first
## [1] 1 2 3 4 5## $second
## [1] "one" "two" "three"## $third
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20## $fourth
## [1] TRUE## $fifth
## [1] "Student"
mylist <- mylist[c(-4, -5)]
mylist
## $first
## [1] 1 2 3 4 5## $second
## [1] "one" "two" "three"## $third
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20
mylist[3] <- NULL
mylist
## $first
## [1] 1 2 3 4 5## $second
## [1] "one" "two" "three"
4.合并列表
yourlist <- list(1:3, 'school')
yourlist
## [[1]]
## [1] 1 2 3## [[2]]
## [1] "school"
mylist
## $first
## [1] 1 2 3 4 5## $second
## [1] "one" "two" "three"
lst <- c(mylist, yourlist)
lst
## $first
## [1] 1 2 3 4 5## $second
## [1] "one" "two" "three"## [[3]]
## [1] 1 2 3## [[4]]
## [1] "school"
class(lst)
## [1] "list"
5.列表转化为向量
mylist
## $first
## [1] 1 2 3 4 5## $second
## [1] "one" "two" "three"
unlist():将list结构的数据,变成非list的数据,即将list数据变成字符串向量或者数字向量的形式。
unlist(mylist)
## first1 first2 first3 first4 first5 second1 second2 second3
## "1" "2" "3" "4" "5" "one" "two" "three"
lst
## $first
## [1] 1 2 3 4 5## $second
## [1] "one" "two" "three"## [[3]]
## [1] 1 2 3## [[4]]
## [1] "school"
unlist(lst)
## first1 first2 first3 first4 first5 second1 second2 second3
## "1" "2" "3" "4" "5" "one" "two" "three" "1"
##
## "2" "3" "school"