R语言02-数据结构2

  • 向量
  • 矩阵
  • 数组
  • 数据框
  • 列表

向量

用于存储数值型、字符型或逻辑型数据的一维数组
  • 创建:函数c()
a <- c(1, 2, 5, 3, 6, -2, 4)       #数值型
b <- c("one", "two", "three")      #字符型
c <- c(TRUE, TRUE, TRUE, FALSE, TRUE, FALSE)   #逻辑型

注意,单个向量中的数据必须拥有相同的类型或模式(数值型、字符型或逻辑型)。

  • 索引:方括号[]
> a <- c("k", "j", "h", "a", "c", "m")
> a[3]
[1] "h"
> a[c(1, 3, 5)]
[1] "k" "h" "c"
> a[2:6]
[1] "j" "h" "a" "c" "m"

a <- c(2:6)等价于a <- c(2,3, 4, 5, 6)

——————————————————————————

矩阵

用于存储数值型、字符型或逻辑型数据的二维数组
  • 创建:函数matrix()
myymatrix <- matrix(vector, nrow=number_of_rows, ncol=number_of_columns,           #指定行和列的维数
byrow=logical_value,      #表明矩阵应当按行填充还是按列填充(byrow=FALSE),默认情况下按列填充
dimnames=list(char_vector_rownames, char_vector_colnames))      #包含了可选的、以字符型向量表示的行名和列名
mymatrix <- matrix(cells, nrow=2, ncol=2, byrow=TRUE,
dimnames=list(rnames, cnames))
> mymatrix
C1 C2
R1 1 26
R2 24 68
  • 索引:使用下标和方括号来选择矩阵中的行、列或元素
> x <- matrix(1:10, nrow=2)
> x
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
> x[1, c(4,5)]
[1] 7 9

——————————————————————————

数组

与矩阵类似,但是维度可以大于2
  • 创建:函数array()
myarray <- array(vector, dimensions, dimnames)

其中vector包含了数组中的数据,dimensions是一个数值型向量,给出了各个维度下标的最大值(几行几列几维),而dimnames是可选的、各维度名称标签的列表。

> dim1 <- c("A1", "A2")
> dim2 <- c("B1", "B2", "B3")
> dim3 <- c("C1", "C2", "C3", "C4")
> z <- array(1:24, c(2, 3, 4), dimnames=list(dim1, dim2, dim3))
> z
, , C1
B1 B2 B3
A1 1 3 5
A2 2 4 6
, , C2
B1 B2 B3
A1 7 9 11
A2 8 10 12
, , C3
B1 B2 B3
A1 13 15 17   #15
A2 14 16 18
, , C4
B1 B2 B3
A1 19 21 23
A2 20 22 24
  • 索引:使用下标和方括号来选择矩阵中的行、列或元素
z[1,2,3]   #为15

——————————————————————————

数据框

由于不同的列可以包含不同模式(数值型、字符型等)的数据,数据框的概念较矩阵来说更为一般
  • 创建:函数data.frame()
mydata <- data.frame(col1, col2, col3,...)

其中的列向量col1、col2、col3等可为任何类型(如字符型、数值型或逻辑型)

> patientID <- c(1, 2, 3, 4)
> age <- c(25, 34, 28, 52)
> diabetes <- c("Type1", "Type2", "Type1", "Type1")
> status <- c("Poor", "Improved", "Excellent", "Poor")
> patientdata <- data.frame(patientID, age, diabetes, status)
> patientdata
patientID age diabetes status
1 1 25 Type1 Poor
2 2 34 Type2 Improved
3 3 28 Type1 Excellent
4 4 52 Type1 Poor
  • 索引:下标记号/指定列名。
> patientdata[1:2]

> patientdata[c("diabetes", "status")]

> patientdata$age
  1. 联合使用函数attach()、detach()和with()
    函数attach()可将数据框添加到R的搜索路径中。
summary(mtcars$mpg)
plot(mtcars$mpg, mtcars$disp)
plot(mtcars$mpg, mtcars$wt)
以上代码也可写成:
attach(mtcars)
summary(mpg)
plot(mpg, disp)
plot(mpg, wt)
detach(mtcars)           #函数detach()将数据框从搜索路径中移除

当名称相同的对象不止一个时,这种方法的局限性就很明显了。(不知道引用哪个)

> mpg <- c(25, 36, 47)
> attach(mtcars)
The following object(s) are masked _by_ '.GlobalEnv': mpg
> plot(mpg, wt)
Error in xy.coords(x, y, xlabel, ylabel, log) :
'x' and 'y' lengths differ
> mpg
[1] 25 36 47

使用函数with(),花括号{}之间的语句都针对数据框mtcars执行。

with(mtcars, {
    print(summary(mpg))
    plot(mpg, disp)
    plot(mpg, wt)
})

函数with()的局限性在于,赋值仅在此函数的括号内生效。(如果想生成全局变量考虑<<—)

> with(mtcars, {
    nokeepstats <- summary(mpg)
    keepstats <<- summary(mpg)
})
> nokeepstats
Error: object 'nokeepstats' not found
> keepstats
Min. 1st Qu. Median Mean 3rd Qu. Max.
10.40 15.43 19.20 20.09 22.80 33.90
  1. 实例标识符
    在R中,实例标识符(case identifier)可通过数据框操作函数中的rowname选项指定。
patientdata <- data.frame(patientID, age, diabetes,status, row.names=patientID)

——————————————————————————

列表

列表就是一些对象(或成分,component)的有序集合。列表允许你整合若干(可能无关的)对象到单个对象名下

某个列表中可能是若干向量、矩阵、数据框,甚至其他列表的组合。

  • 创建:函数list()
mylist <- list(object1, object2, ...)           #其中的对象可以是目前为止讲到的任何结构

#你还可以为列表中的对象命名:
mylist <- list(name1=object1, name2=object2, ...)
> g <- "My First List"
> h <- c(25, 26, 18, 39)
> j <- matrix(1:10, nrow=5)
> k <- c("one", "two", "three")
> mylist <- list(title=g, ages=h, j, k)
> mylist
$title
[1] "My First List"
$ages
[1] 25 26 18 39
[[3]]
[,1] [,2]
[1,] 1 6
[2,] 2 7
[3,] 3 8
[4,] 4 9
[5,] 5 10
[[4]]
[1] "one" "two" "three"
> mylist[[2]]
[1] 25 26 18 39
> mylist[["ages"]]
[[1] 25 26 18 39
  • 索引:双重方括号中指明代表某个成分的数字或名称来访问列表中的元素
mylist[[2]]
#和
mylist[["ages"]]
#均指那个含有四个元素的向量。对于命名成分,
mylist$ages  #也可以正常运行。
发布了8 篇原创文章 · 获赞 0 · 访问量 58

猜你喜欢

转载自blog.csdn.net/xiuxiuxiu666/article/details/104231730
今日推荐