R语言数据结构之列表

目录

1.创建列表

2.访问列表 

3.修改列表 

(1)增加元素

(2)修改元素 

 (3)删除元素

 4.合并列表

 5.列表转化为向量


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" 

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可乐  

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值