当前位置:   article > 正文

R语言数据结构之列表_r将字符串改为列表

r将字符串改为列表

目录

1.创建列表

2.访问列表 

3.修改列表 

(1)增加元素

(2)修改元素 

 (3)删除元素

 4.合并列表

 5.列表转化为向量


1.创建列表

  1. # 向量元素类型必须一致,按优先级自动转换
  2. data1 <-c(66,'student',TRUE)
  3. data1

##  [1] "66"      "student" "TRUE" 

  1. #列表元素类型可能不同
  2. data2 <-list(66,'student',TRUE)
  3. data2

##  [[1]]
##  [1] 66

##  [[2]]
##  [1] "student"

##  [[3]]
##  [1] TRUE

str():可以紧凑的显示对象内部结构

str(data2)  

##   List of 3
##   $ : num 66
##   $ : chr "student"
##   $ : logi TRUE

  1. a <- 1:5
  2. b <- c('one', 'two', 'three')
  3. c <- matrix(1:20, 4, 5)
  4. mylist <- list(a, b, c)
  5. 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 ...

  1. # 给列表元素添加名称
  2. mylist <- list(first=a, second=b, third=c)
  3. 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.访问列表 

  1. # 长度信息
  2. length(mylist)

##  [1] 3 

  1. # 名称信息
  2. names(mylist)

 ##  [1] "first"  "second" "third" 

  1. # 返回值为列表
  2. 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():查看数据结构 

  1. #class(mylist[1])
  2. #class(mylist[2])
  3. 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"

  1. # 返回值为元素本身类型
  2. 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" 

  1. # 返回值为元素本身类型
  2. mylist[['first']]
##  [1] 1 2 3 4 5
class(mylist[['first']])
 ##  [1] "integer"

  1. # 返回值为元素本身类型
  2. mylist$first
##  [1] 1 2 3 4 5
class(mylist$first)
##  [1] "integer"

  1. # 访问多个元素
  2. # mylist(1, 3) #注意:写法有误
  3. # mylist[1, 3] #注意:写法有误
  4. # mylist[[1, 3]] #注意:写法有误
  5. 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)增加元素

  1. mylist[4] <- 'This is a list'
  2. mylist['fourth'] <- 34
  3. mylist$fifth <- FALSE
  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

##  [[4]]
##  [1] "This is a list"

##  $fourth
##  [1] 34

##  $fifth
##  [1] FALSE
 

(2)修改元素 

  1. mylist[4] <- 8888
  2. mylist['fourth'] <- TRUE
  3. mylist$fifth <- 'Student'
  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

##  [[4]]
##  [1] 8888

##  $fourth
##  [1] TRUE

##  $fifth
##  [1] "Student"

 (3)删除元素

  1. mylist <- mylist[-4]
  2. 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"

  1. mylist <- mylist[c(-4, -5)]
  2. 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

  1. mylist[3] <- NULL
  2. mylist

##  $first
##  [1] 1 2 3 4 5

##  $second
##  [1] "one"   "two"   "three"

 4.合并列表

  1. yourlist <- list(1:3, 'school')
  2. yourlist

##  [[1]]
##  [1] 1 2 3

##  [[2]]
##  [1] "school"

mylist

##  $first
##  [1] 1 2 3 4 5

##  $second
##  [1] "one"   "two"   "three"

  1. lst <- c(mylist, yourlist)
  2. 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" 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/702601
推荐阅读
相关标签
  

闽ICP备14008679号