R语言学习-矩阵

2024-07-14 15:21:19 浏览数 (2)

矩阵

矩阵(matrix)是一种特殊的向量,包含两个附加的属性:行数和列数,R生存矩阵按列存储

注意:向量不能看成只有一行或者一列的矩阵,二者不能混为一谈

矩阵的创建

  1. 利用matrix()函数 例如 matrix(data = NA,nrow = 1,ncol = 1,byrow = FALSE,dimnames = NULL);

data表示要处理的数据(向量)

nrow表示行

ncol表示列

byrow表示是否按行排列,默认是列排

dimnames表示行与列的名字,默认是没有的,要使用列表设置

代码语言:R复制
> mydata <- matrix(c(1,2,3,4,5,6),nrow = 2,ncol = 3)#两行三列
> mydata
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6
> mydata1 <- matrix(c(1,2,3,4,5,6),nrow = 2,ncol = 3,byrow = T)
> mydata1
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
> mydata3 <- matrix(c(1,2,3,4,5,6),nrow = 2,ncol = 3,dimnames = list(c("r1","r2"),c("c1","c2","c3")))
> mydata3
   c1 c2 c3
r1  1  3  5
r2  2  4  6

2.利用函数将向量接起来

rbind()函数:按行接起来

cbind()函数:按列接起来

代码语言:R复制
> mydata5 <- rbind(c(1,2,3),c(4,5,6))
> mydata6 <- cbind(c(1,2,3),c(4,5,6))
> mydata5
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
> mydata6
     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

> mydata7 <- rbind(c(1,2,3,4,5),c(4,5,6))#出现警告,循环补齐
Warning message:
In rbind(c(1, 2, 3, 4, 5), c(4, 5, 6)) :
  number of columns of result is not a multiple of vector length (arg 2)
> mydata7
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    4    5    6    4    5

> mydata8 <- cbind(c(1,2,3),c(4,5,6,7,8))
Warning message:
In cbind(c(1, 2, 3), c(4, 5, 6, 7, 8)) :
  number of rows of result is not a multiple of vector length (arg 1)
> mydata8
     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6
[4,]    1    7
[5,]    2    8

3.强制转换为矩阵

利用as.matrix()函数

代码语言:R复制
> a <- c(1,2,3,4,5)
> a1 <- as.matrix(a)
> a1
     [,1]
[1,]    1
[2,]    2
[3,]    3
[4,]    4
[5,]    5

4.建议一个空矩阵

直接写行列大小

代码语言:R复制
> b <- matrix(nrow = 3,ncol = 2)
> b
     [,1] [,2]
[1,]   NA   NA
[2,]   NA   NA
[3,]   NA   NA

注意:当向量中含有不同类型的数据时,会改变元素类型后转变为矩阵

代码语言:R复制
> mydata10 <- matrix(c(2,3,FALSE,TRUE),2,2) #向量中含有逻辑与数值型,逻辑转为数值,2行2列排序
> mydata10
     [,1] [,2]
[1,]    2    0
[2,]    3    1
> #当向量含有逻辑、字符、数值型,全部转换为字符
> mydata11 <- matrix(c(2,3,5,7,"ABC",FALSE,TRUE,3,9,3,4,5),4,3)
> mydata11
     [,1] [,2]    [,3]
[1,] "2"  "ABC"   "9" 
[2,] "3"  "FALSE" "3" 
[3,] "5"  "TRUE"  "4" 
[4,] "7"  "3"     "5" 

矩阵的索引

使用下标和中括号来选择矩阵中的行或列或元素,矩阵名 中括号,中括号里面写需要的行与列

例如,设置一个矩阵,名字为mymatrix,5行5列

代码语言:R复制
> mymatrix <- matrix(c(1:25),5,5)
> mymatrix
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16   21
[2,]    2    7   12   17   22
[3,]    3    8   13   18   23
[4,]    4    9   14   19   24
[5,]    5   10   15   20   25
> #矩阵每一行前或列首,都有一个中括号,可以理解为这一行或列标
> #mymatrix[i,],返回矩阵mymatrix中第i行元素
> mymatrix[3,]
[1]  3  8 13 18 23
> #mymatrix[,j],返回矩阵mymatrix中第j列元素
> mymatrix[,5]
[1] 21 22 23 24 25
> mymatrix[3,4]
[1] 18
> mymatrix[2,-5]#返回第i行,但是排除第j列
[1]  2  7 12 17
> mymatrix[c(1,3),c(3,5)]#返回第1和3行,第3和5列
     [,1] [,2]
[1,]   11   21
[2,]   13   23
> mymatrix[-c(1,3),c(3,5)]#返回第3和5列,但是排除第1行和第3行
     [,1] [,2]
[1,]   12   22
[2,]   14   24
[3,]   15   25

#当矩阵有行列名时,可以通过行与列名进行提取元素
> mymatrix <- matrix(c(1:25),5,5,dimnames = list(c("r1","r2","r3","r4","r5"),c("c1","c2","c3","c4","c5")))
> mymatrix
   c1 c2 c3 c4 c5
r1  1  6 11 16 21
r2  2  7 12 17 22
r3  3  8 13 18 23
r4  4  9 14 19 24
r5  5 10 15 20 25
> mymatrix["r4","c1"]
[1] 4
> mymatrix[c("r3","r5"),c("c1","c3")]
   c1 c3
r3  3 13
r5  5 15

drop处理意外降维

上面矩阵的引用1~5条,从一个矩阵中提取的结果都为向量,如果说,我取一行或者一列返回结果要求是矩阵,虽然说可以先去后变为矩阵 但是相对麻烦,我们可以加入drop防止降维

代码语言:R复制
> mymatrix1 <- matrix(c(1:36),6,6,dimnames = list(c("r1","r2","r3","r4","r5","r6"),c("c1","c2","c3","c4","c5","c6")))
> mymatrix1
   c1 c2 c3 c4 c5 c6
r1  1  7 13 19 25 31
r2  2  8 14 20 26 32
r3  3  9 15 21 27 33
r4  4 10 16 22 28 34
r5  5 11 17 23 29 35
r6  6 12 18 24 30 36
> mymatrix1[2,]#直接取会降维为向量
c1 c2 c3 c4 c5 c6 
 2  8 14 20 26 32 
> mymatrix1[6,,drop=F]#如果想取出是矩阵,则加入drop=F
   c1 c2 c3 c4 c5 c6
r6  6 12 18 24 30 36

矩阵相关操作及相关函数

1.转置,函数t()

代码语言:R复制
> #首先创建一个矩阵
> mymatrix <- matrix(c(1:36),6,6,dimnames = list(c("r1","r2","r3","r4","r5","r6"),c("c1","c2","c3","c4","c5","c6")))
> mymatrix
   c1 c2 c3 c4 c5 c6
r1  1  7 13 19 25 31
r2  2  8 14 20 26 32
r3  3  9 15 21 27 33
r4  4 10 16 22 28 34
r5  5 11 17 23 29 35
r6  6 12 18 24 30 36
> #通过t()函数进行转置,返回结果如下
> mymatrix3 <- t(mymatrix)
> mymatrix3
   r1 r2 r3 r4 r5 r6
c1  1  2  3  4  5  6
c2  7  8  9 10 11 12
c3 13 14 15 16 17 18
c4 19 20 21 22 23 24
c5 25 26 27 28 29 30
c6 31 32 33 34 35 36

2.横向或者纵向合并矩阵

cbind()函数横向合并矩阵,rbind()函数纵向合并矩阵

代码语言:R复制
> M <- matrix(c(1:36),6,6,dimnames = list(c("r1","r2","r3","r4","r5","r6"),c("c1","c2","c3","c4","c5","c6")))
> M
   c1 c2 c3 c4 c5 c6
r1  1  7 13 19 25 31
r2  2  8 14 20 26 32
r3  3  9 15 21 27 33
r4  4 10 16 22 28 34
r5  5 11 17 23 29 35
r6  6 12 18 24 30 36
> N <- matrix(c(37:72),6,6,dimnames = list(c("a1","a2","a3","a4","a5","a6"),c("b1","b2","b3","b4","b5","b6")))
> N
   b1 b2 b3 b4 b5 b6
a1 37 43 49 55 61 67
a2 38 44 50 56 62 68
a3 39 45 51 57 63 69
a4 40 46 52 58 64 70
a5 41 47 53 59 65 71
a6 42 48 54 60 66 72
> A <- cbind(M,N)
> B <- rbind(M,N)
> A
   c1 c2 c3 c4 c5 c6 b1 b2 b3 b4 b5 b6
r1  1  7 13 19 25 31 37 43 49 55 61 67
r2  2  8 14 20 26 32 38 44 50 56 62 68
r3  3  9 15 21 27 33 39 45 51 57 63 69
r4  4 10 16 22 28 34 40 46 52 58 64 70
r5  5 11 17 23 29 35 41 47 53 59 65 71
r6  6 12 18 24 30 36 42 48 54 60 66 72
> B
   c1 c2 c3 c4 c5 c6
r1  1  7 13 19 25 31
r2  2  8 14 20 26 32
r3  3  9 15 21 27 33
r4  4 10 16 22 28 34
r5  5 11 17 23 29 35
r6  6 12 18 24 30 36
a1 37 43 49 55 61 67
a2 38 44 50 56 62 68
a3 39 45 51 57 63 69
a4 40 46 52 58 64 70
a5 41 47 53 59 65 71
a6 42 48 54 60 66 72

3.对各列或行求和或求均值

colSums()对各列求和,rowMeans()对各行求均值

代码语言:R复制
> D <- colSums(M)
> E <- rowMeans(N)
> D
 c1  c2  c3  c4  c5  c6 
 21  57  93 129 165 201 
> E
a1 a2 a3 a4 a5 a6 
52 53 54 55 56 57

4.计算行列式

det()函数,计算行列式

代码语言:R复制
> U1 <- matrix(c(1:4),2,2,dimnames = list(c("r1","r2"),c("c1","c2")))
> U2 <- det(U1)
> U2
[1] -2

5.矩阵相乘

使用%*%进行矩阵相乘

代码语言:R复制
> U3 <- matrix(c(5:8),2,2)
> K <- U1%*%U3
> K
   [,1] [,2]
r1   23   31
r2   34   46

6.设置行列名

设置行列名有三种以上的方式,介绍三种常用的

代码语言:R复制
> #使用dimnames,以列表的形式设置行列名
> fa <- matrix(c(1:4),2,2,dimnames = list(c("第一行","第二行"),c("第一列","第二列")))
> fa
       第一列 第二列
第一行      1      3
第二行      2      4
> #在外部,使用dimnames()函数设置行列名
> f1 <- matrix(c(6:9), nrow = 2, ncol = 2)
> a <- c("第一行","第二行")
> b <- c("第一列","第二列")
> dimnames(f1) <- list(a,b)
> f1
       第一列 第二列
第一行      6      8
第二行      7      9
> #使用函数rownames()设置行名,使用colnames()设置列名
> f2 <- matrix(c(1,3,5,7),2,2)
> rownames(f2) <- c("A1","A2")
> colnames(f2) <- c("B1","B2")
> f2
   B1 B2
A1  1  5
A2  3  7

apply()函数

apply(m,dimcode,fargs),允许用户在各行各列调用函数

m 矩阵,dimcode,1代表行,2代表列,f函数,frags可选参数,函数大于一个参考时使用“,”隔开

代码语言:R复制
> mymatrix <- matrix(c(1:25),5,5)
> mymatrix
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16   21
[2,]    2    7   12   17   22
[3,]    3    8   13   18   23
[4,]    4    9   14   19   24
[5,]    5   10   15   20   25
> mymatrix_rowmean <- apply(mymatrix,1,mean) #对行求均值
> mymatrix_rowmean
[1] 11 12 13 14 15
> mymatrix_colmean <- apply(mymatrix,2,mean) #对列求均值
> mymatrix_colmean
[1]  3  8 13 18 23
> mymatrix_rowmean1 <- matrix(mymatrix_rowmean,length(mymatrix_rowmean) ,1)#转换为一列的矩阵
> mymatrix_rowmean1
     [,1]
[1,]   11
[2,]   12
[3,]   13
[4,]   14
[5,]   15
> mymatrix_colmean1 <- matrix(mymatrix_colmean,1,length(mymatrix_colmean))#转换为一行的矩阵
> mymatrix_colmean1
     [,1] [,2] [,3] [,4] [,5]
[1,]    3    8   13   18   23
> mymatrix_colmean[6] <- NA #矩阵右下角有个空,使用NA值填一下,方便连接
> A <- cbind(mymatrix,mymatrix_rowmean1) #按照列把两个矩阵连接起来
> B <- rbind(A,mymatrix_colmean1) #按照行把两个矩阵连接起来
Error in rbind(A, mymatrix_colmean1) : 矩阵的列数必需相符(见arg2)
> rm(list = ls())
> mymatrix <- matrix(c(1:25),5,5)
> mymatrix
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16   21
[2,]    2    7   12   17   22
[3,]    3    8   13   18   23
[4,]    4    9   14   19   24
[5,]    5   10   15   20   25
> mymatrix_rowmean <- apply(mymatrix,1,mean) #对行求均值
> mymatrix_rowmean
[1] 11 12 13 14 15
> mymatrix_colmean <- apply(mymatrix,2,mean) #对列求均值
> mymatrix_colmean
[1]  3  8 13 18 23
> mymatrix_rowmean1 <- matrix(mymatrix_rowmean,length(mymatrix_rowmean) ,1)#转换为一列的矩阵
> mymatrix_rowmean1
     [,1]
[1,]   11
[2,]   12
[3,]   13
[4,]   14
[5,]   15
> mymatrix_colmean1 <- matrix(mymatrix_colmean,1,length(mymatrix_colmean))#转换为一行的矩阵
> mymatrix_colmean1
     [,1] [,2] [,3] [,4] [,5]
[1,]    3    8   13   18   23
> mymatrix_colmean1[6] <- NA #矩阵右下角有个空,使用NA值填一下,方便连接
> A <- cbind(mymatrix,mymatrix_rowmean1) #按照列把两个矩阵连接起来
> B <- rbind(A,mymatrix_colmean1) #按照行把两个矩阵连接起来
> a <- c("r1","r2","r3","r4","r5","列均值")
> b <- c("a1","a3","a3","a4","a5","行均值",)
Error in c("a1", "a3", "a3", "a4", "a5", "行均值", ) : 7元素是空的
> rm(list = ls())
> mymatrix <- matrix(c(1:25),5,5)
> mymatrix
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    6   11   16   21
[2,]    2    7   12   17   22
[3,]    3    8   13   18   23
[4,]    4    9   14   19   24
[5,]    5   10   15   20   25
> mymatrix_rowmean <- apply(mymatrix,1,mean) #对行求均值
> mymatrix_rowmean
[1] 11 12 13 14 15
> mymatrix_colmean <- apply(mymatrix,2,mean) #对列求均值
> mymatrix_colmean
[1]  3  8 13 18 23
> mymatrix_rowmean1 <- matrix(mymatrix_rowmean,length(mymatrix_rowmean) ,1)#转换为一列的矩阵
> mymatrix_rowmean1
     [,1]
[1,]   11
[2,]   12
[3,]   13
[4,]   14
[5,]   15
> mymatrix_colmean1 <- matrix(mymatrix_colmean,1,length(mymatrix_colmean))#转换为一行的矩阵
> mymatrix_colmean1
     [,1] [,2] [,3] [,4] [,5]
[1,]    3    8   13   18   23
> mymatrix_colmean1[6] <- NA #矩阵右下角有个空,使用NA值填一下,方便连接
> A <- cbind(mymatrix,mymatrix_rowmean1) #按照列把两个矩阵连接起来
> B <- rbind(A,mymatrix_colmean1) #按照行把两个矩阵连接起来
> a <- c("r1","r2","r3","r4","r5","列均值")
> b <- c("a1","a2","a3","a4","a5","行均值")
> dimnames(B) <- list(a,b)
> B
       a1 a2 a3 a4 a5 行均值
r1      1  6 11 16 21     11
r2      2  7 12 17 22     12
r3      3  8 13 18 23     13
r4      4  9 14 19 24     14
r5      5 10 15 20 25     15
列均值  3  8 13 18 23     NA

数据结构的查看

可以使用class()函数查看数据结构,如何判断对象是否是矩阵呢?

1.使用attributes()函数,若是矩阵返回一个维度,若不是,返回NULL值

代码语言:R复制
> fa <- matrix(c(1:4),2,2) #返回维度,2行2列
> attributes(fa)
$dim
[1] 2 2

> attributes(fa[2,])#向量无维度,返回NULL值
NULL
> is.matrix(fa) #判断fa是不是一个矩阵,若是返回TRUE,不是返回NULL
[1] TRUE
> is.vector(fa[2,]) #判断fa是不是一个向量,若是返回TRUE,不是返回NULL
[1] TRUE

0 人点赞