参考视频:https://www.bilibili.com/video/BV19x411X7C6?t=2&p=20
一、矩阵
矩阵(Matrix):是一个按照长方阵列排列的复数或实数集合。向量是一维的,矩阵是二维的,需要有行和列。
在R中,矩阵是有维数的向量,矩阵元素可以使数值型、字符型或逻辑型,但每个元素必须是相同模式,这个和向量一致。
很有名的鸢尾花数据集就是一个矩阵。
- 创建矩阵
> x <- 1:20
> m <- matrix(x,nrow=4,ncol=5)
> m
[,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
> matrix(1:20,4,5)
[,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
> m <- matrix(1:20,4,byrow = T)//byrow=true,按行排列
> m
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 6 7 8 9 10
[3,] 11 12 13 14 15
[4,] 16 17 18 19 20
> m <- matrix(1:20,4,byrow = F)//byrow=false,按列排列
[,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
#定义行列名称
> rnames <- c("R1","R2","R3","R4")
> cnames <- c("C1","C2","C3","C4","C5")
> dimnames(m) <- list(rnames,cnames)//dim=dimension,维数
> m
C1 C2 C3 C4 C5
R1 1 5 9 13 17
R2 2 6 10 14 18
R3 3 7 11 15 19
R4 4 8 12 16 20
dim函数可以显示向量的维数
> dim(x)
NULL
> dim(x) <- c(4,5)
> x//由向量变成了矩阵
[,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
二、数组
R中的向量~其他语言中的数组
R中的数组~多维矩阵
1.创建数组
(1)dim()
#定义三维数组
> x <- 1:20
> dim(x) <- c(2,2,5)
> x
, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2
[,1] [,2]
[1,] 5 7
[2,] 6 8
, , 3
[,1] [,2]
[1,] 9 11
[2,] 10 12
, , 4
[,1] [,2]
[1,] 13 15
[2,] 14 16
, , 5
[,1] [,2]
[1,] 17 19
[2,] 18 20
(2)array()
> dim1 <- c("A1","A2")
> dim2 <- c("B1","B2","B3")
> dim3 <- c("C1","C2","C3","C4")
> z <- array(1:24,dim = 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
A2 14 16 18
, , C4
B1 B2 B3
A1 19 21 23
A2 20 22 24
三、矩阵索引(定位具体数据值)
- 可通过矩阵下标进行访问
R使用线性代数中i,j
i:表示行号 j:表示列号
> m <- matrix(1:20,4,5,byrow = T)
> m
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 6 7 8 9 10
[3,] 11 12 13 14 15
[4,] 16 17 18 19 20
> m[1,2]//访问第一行第二列
[1] 2
> m[1,c(2,3,4)]//访问第一行的第2 3 4列
[1] 2 3 4
> m[c(2,3,4),c(2,3)]//访问2,3,4行的2、3列(子集)
[,1] [,2]
[1,] 7 8
[2,] 12 13
[3,] 17 18
> m[2,]//访问第二行
[1] 6 7 8 9 10
> m[,2]//访问第二列
[1] 2 7 12 17
> m[-1,2]//访问非第一行的第二列(不包括第一行的第二列)
[1] 7 12 17
- 通过行名列名来访问
名称是字符串,要添加引号
> dimnames(m)=list (rnames,cnames)
> m
C1 C2 C3 C4 C5
R1 1 2 3 4 5
R2 6 7 8 9 10
R3 11 12 13 14 15
R4 16 17 18 19 20
> m["R1","C2"]
[1] 2
例子:state.x77矩阵
> head(state.x77)
Population Income Illiteracy Life Exp Murder HS Grad Frost Area
Alabama 3615 3624 2.1 69.05 15.1 41.3 20 50708
Alaska 365 6315 1.5 69.31 11.3 66.7 152 566432
Arizona 2212 4530 1.8 70.55 7.8 58.1 15 113417
Arkansas 2110 3378 1.9 70.66 10.1 39.9 65 51945
California 21198 5114 1.1 71.71 10.3 62.6 20 156361
Colorado 2541 4884 0.7 72.06 6.8 63.9 166 103766
> state.x77[,"Income"]
Alabama Alaska Arizona Arkansas California
3624 6315 4530 3378 5114
Colorado Connecticut Delaware Florida Georgia
4884 5348 4809 4815 4091
Hawaii Idaho Illinois Indiana Iowa
4963 4119 5107 4458 4628
Kansas Kentucky Louisiana Maine Maryland
4669 3712 3545 3694 5299
Massachusetts Michigan Minnesota Mississippi Missouri
4755 4751 4675 3098 4254
Montana Nebraska Nevada New Hampshire New Jersey
4347 4508 5149 4281 5237
New Mexico New York North Carolina North Dakota Ohio
3601 4903 3875 5087 4561
Oklahoma Oregon Pennsylvania Rhode Island South Carolina
3983 4660 4449 4558 3635
South Dakota Tennessee Texas Utah Vermont
4167 3821 4188 4022 3907
Virginia Washington West Virginia Wisconsin Wyoming
4701 4864 3617 4468 4566
> state.x77["Alabama",]
Population Income Illiteracy Life Exp Murder HS Grad Frost Area
3615.00 3624.00 2.10 69.05 15.10 41.30 20.00 50708.00
四、矩阵运算
- 若元素都是数值型,则与向量类似(加、减……)
> m+1
C1 C2 C3 C4 C5
R1 2 3 4 5 6
R2 7 8 9 10 11
R3 12 13 14 15 16
R4 17 18 19 20 21
> m+m
C1 C2 C3 C4 C5
R1 2 4 6 8 10
R2 12 14 16 18 20
R3 22 24 26 28 30
R4 32 34 36 38 40
> m*m
C1 C2 C3 C4 C5
R1 1 4 9 16 25
R2 36 49 64 81 100
R3 121 144 169 196 225
R4 256 289 324 361 400
> m/m
C1 C2 C3 C4 C5
R1 1 1 1 1 1
R2 1 1 1 1 1
R3 1 1 1 1 1
R4 1 1 1 1 1
矩阵的运算需要行和列一致
> n <- matrix(1:20,5,4)
> n
[,1] [,2] [,3] [,4]
[1,] 1 6 11 16
[2,] 2 7 12 17
[3,] 3 8 13 18
[4,] 4 9 14 19
[5,] 5 10 15 20
> m
C1 C2 C3 C4 C5
R1 1 2 3 4 5
R2 6 7 8 9 10
R3 11 12 13 14 15
R4 16 17 18 19 20
> m+n
Error in m + n : 非整合陈列
相关计算函数:
colSums(m):计算每一列的和
> colSums(m)
C1 C2 C3 C4 C5
34 38 42 46 50
rowSums(m):计算每一行的和
> rowSums(m)
R1 R2 R3 R4
15 40 65 90
rowMeans(m):平均值
- 矩阵内积、外积
> n <- matrix(1:9,3,3)
> t <- matrix(2:10,3,3)
> n * t //内积
[,1] [,2] [,3]
[1,] 2 20 56
[2,] 6 30 72
[3,] 12 42 90
> n %*% t //外积
[,1] [,2] [,3]
[1,] 42 78 114
[2,] 51 96 141
[3,] 60 114 168
若是不清楚何为内积何为外积,可参考:矩阵的内积、外积
diag(n):返回矩阵n对角线的值
> n
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> diag(n)
[1] 1 5 9
t(m):将矩阵m进行转置,行和列进行互换
> m
C1 C2 C3 C4 C5
R1 1 2 3 4 5
R2 6 7 8 9 10
R3 11 12 13 14 15
R4 16 17 18 19 20
> t(m)
R1 R2 R3 R4
C1 1 6 11 16
C2 2 7 12 17
C3 3 8 13 18
C4 4 9 14 19
C5 5 10 15 20
网友评论