1.创建矩阵
(1)通过matrix()函数创建矩阵
# 默认x轴优先
matrix(1:6, nrow = 2)
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
# 设置Y轴优先
matrix(1:6, nrow = 2, byrow = T)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
# 通过dimnames参数添加行列名称
matrix(1:6,
nrow = 2,
# ncol = 3,
byrow = F,
dimnames = list(c('r1', 'r2'),
c('c1', 'c2', 'c3')))
c1 c2 c3
r1 1 3 5
r2 2 4 6
rownames():添加行名
colnames():添加列名
# 通过rownames()、colnames()函数给矩阵添加行列名称
mat <- matrix(1:6, nrow = 2, byrow = T)
rownames(mat) <- c('R1', 'R2')
colnames(mat) <- c('C1', 'C2', 'C3')
mat
C1 C2 C3
R1 1 2 3
R2 4 5 6
** (2)通过dim()函数创建矩阵,原理是改变维度**
# 产生向量
mat1 <- 1:6
mat1
[1] 1 2 3 4 5 6
# 改变向量维度后成为矩阵
dim(mat1) <- c(2, 3)
mat1
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
is.matrix(mat1) #验证向量是否为矩阵
[1] TRUE
# 按行填充
mat2 <- 1:6
dim(mat2) <- c(3, 2)
mat2 <- t(mat2) # t()换行换列
mat2
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
2.矩阵的属性
# 数据结构类型
class(mat)
[1] "matrix" "array"
# 数据元素类型
typeof(mat)
[1] "integer"
# 数据维度
dim(mat)
[1] 2 3
# 行维度
nrow(mat)
[1] 2
# 列维度
ncol(mat)
[1] 3
# 数据元素个数
length(mat)
[1] 6
# 行名称
rownames(mat)
[1] "R1" "R2"
# 列名称
colnames(mat)
[1] "C1" "C2" "C3"
3.访问矩阵
(1)通过下标访问
# 输出mat矩阵
mat
C1 C2 C3
R1 1 2 3
R2 4 5 6
# 访问第2行第2列元素
mat[2, 2]
[1] 5
# 访问第2行元素
mat[2, ]
C1 C2 C3
4 5 6
# 访问第2列元素
mat[, 2]
R1 R2
2 5
# 返回均为向量结构
#is.vector(mat[2, 2])
is.vector(mat[2, ])
[1] TRUE
# 访问第1-2行,第2-3列数据,返回矩阵
mat[1:2, 2:3]
C2 C3
R1 2 3
R2 5 6
# 访问第1-2行,第1、3列数据,返回矩阵
mat[1:2, c(1,3)]
C1 C3
R1 1 3
R2 4 6
** (2)通过名称访问**
# 访问元素
mat['R2', 'C2']
[1] 5
# 访问R2行
mat['R2',]
C1 C2 C3
4 5 6
# 访问C2列
mat[, 'C2']
R1 R2
2 5
# 访问'R1'、'R2'行,'C1'、'C3'列,返回矩阵
mat[c('R1', 'R2'), c('C1', 'C3')]
C1 C3
R1 1 3
R2 4 6
** (3)访问后返回矩阵**
mat[2, 2, drop = F]
C2
R2 5
mat[2, , drop = F]
C1 C2 C3
R2 4 5 6
#is.matrix(mat[2, 2, drop = F])
is.matrix(mat[2, , drop = F]) #查看是否为矩阵
[1] TRUE
** 4.编辑矩阵中的元素**
# 将第2行第2列数据修改成66
mat[2, 2] <- 66
mat
C1 C2 C3
R1 1 2 3
R2 4 66 6
# 将第3列数据修改成11
mat[,3] <- 11
mat
C1 C2 C3
R1 1 2 11
R2 4 66 11
# 将矩阵中小于4的元素修改成44
mat[mat<4] <- 44
mat
C1 C2 C3
R1 44 44 11
R2 4 66 11
# 删除第3列数据
mat <- mat[, -3]
mat
C1 C2
R1 44 44
R2 4 66
mat <- matrix(1:9, nrow = 3)
mat
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
# 添加一行数据
rbind(mat, c(12, 13, 14))
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
[4,] 12 13 14
# 添加一列数据
cbind(mat, c(12, 13, 14))
[,1] [,2] [,3] [,4]
[1,] 1 4 7 12
[2,] 2 5 8 13
[3,] 3 6 9 14
5.矩阵的基本运算
(1)矩阵间的运算
X = matrix(seq(1, 9 , 2), nrow = 2)
Warning in matrix(seq(1, 9, 2), nrow = 2): 数据长度[5]不是矩阵行数[2]的整倍
X = matrix(seq(1, 10, 2), nrow = 2)
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 3 7 11
Y = matrix(seq(2, 10, 12), nrow = 2)
[,1] [,2] [,3]
[1,] 2 6 10
[2,] 4 8 12
# 对应位置进行计算
# 四则运算X+Y、X-Y、X*Y、X/Y
X + Y
[,1] [,2] [,3]
[1,] 3 11 19
[2,] 7 15 23
(2)矩阵与向量的运算
matrix(1:4, nrow = 2)
[,1] [,2]
[1,] 1 3
[2,] 2 4
# 数字3会自动补齐为matrix(c(3, 3, 3, 3), nrow = 2)
matrix(1:4, nrow = 2) + 3
[,1] [,2]
[1,] 4 6
[2,] 5 7
# 向量c(1, 2)自动补齐matrix(c(1, 2, 1, 2), nrow = 2)
matrix(1:4, nrow = 2) + c(1, 2)
[,1] [,2]
[1,] 2 4
[2,] 4 6
matrix(1:4, nrow = 2) + c(0, 2, 4,0)
[,1] [,2]
[1,] 1 7
[2,] 4 4
# 向量长度不能超过矩阵元素个数
# matrix(1:4, nrow = 2) + c(0, 2, 4, 6, 8)
** (3)其它运算**
#cos(matrix(1:4, nrow = 2))
sin(matrix(1:4, nrow = 2)) #三角函数运算
[,1] [,2]
[1,] 0.8414710 0.1411200
[2,] 0.9092974 -0.7568025
# max(matrix(1:4, nrow = 2))
# min(matrix(1:4, nrow = 2))
# mean(matrix(1:4, nrow = 2))
exp(matrix(1:4, nrow = 2)) #指数运算
[,1] [,2]
[1,] 2.718282 20.08554
[2,] 7.389056 54.59815
** 6.矩阵的转置**
# 产生向量
c(1:4)
[1] 1 2 3 4
# 使用t()函数将向量转换成行矩阵
t(c(1:4))
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
# 使用t()函数将向量转换两次可得到列矩阵
t(t(c(1:4)))
[,1]
[1,] 1
[2,] 2
[3,] 3
[4,] 4
** 7.矩阵对角元素相关操作**
# 矩阵对角元素
M <- matrix(1:9, nrow = 3)
diag(M) #构造一个对角矩阵
[1] 1 5 9
diag(M) <- c(66, 77, 88)
M
[,1] [,2] [,3]
[1,] 66 4 7
[2,] 2 77 8
[3,] 3 6 88
# 创建对角矩阵
diag(c(1, 2, 3, 4, 5))
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 0 0 0
[2,] 0 2 0 0 0
[3,] 0 0 3 0 0
[4,] 0 0 0 4 0
[5,] 0 0 0 0 5
# 创建单位矩阵
diag(9)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 1 0 0 0 0 0 0 0 0
[2,] 0 1 0 0 0 0 0 0 0
[3,] 0 0 1 0 0 0 0 0 0
[4,] 0 0 0 1 0 0 0 0 0
[5,] 0 0 0 0 1 0 0 0 0
[6,] 0 0 0 0 0 1 0 0 0
[7,] 0 0 0 0 0 0 1 0 0
[8,] 0 0 0 0 0 0 0 1 0
[9,] 0 0 0 0 0 0 0 0 1
** 8.矩阵转化为向量**
M <- matrix(1:9, nrow = 3)
M
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
# 默认按列转化成向量
as.vector(M)
[1] 1 2 3 4 5 6 7 8 9
c(M)
[1] 1 2 3 4 5 6 7 8 9
# 利用t()函数按行转化成向量
as.vector(t(M))
[1] 1 4 7 2 5 8 3 6 9
c(t(M))
[1] 1 4 7 2 5 8 3 6 9
**9.矩阵的行列计算 **
M <- matrix(1:12, nrow = 3)
M
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
# 计算第2列的平均值
mean(M[, 2])
[1] 5
# 计算第2行的方差
var(M[2,])
[1] 15
# 计算行和
rowSums(M)
[1] 22 26 30
apply()函数用于处理矩阵类型的数据,一般有三个参数;第一个参数代表对其应用操作的矩阵,第二个参数代表允许我们指定是按行还是按列应用操作(1表示对行,2表示对列),第三个参数代表处理数据的函数。
apply(M, 1, sum)
[1] 22 26 30
# 计算列和
colSums(M)
[1] 6 15 24 33
apply(M, 2, sum)
[1] 6 15 24 33
# 计算行平均值
rowMeans(M)
[1] 5.5 6.5 7.5
apply(M, 1, mean)
[1] 5.5 6.5 7.5
# 计算列平均值
colMeans(M)
[1] 2 5 8 11
apply(M, 2, mean)
[1] 2 5 8 11
# 计算每行最大值
apply(M, 1, max)
[1] 10 11 12
# 计算每列最小值
apply(M, 2, min)
[1] 1 4 7 10
# 如果矩阵中存在NA值 ,可以设置na.rm=T忽略NA值
M[2, 2] <- NA
M
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 NA 8 11
[3,] 3 6 9 12
#计算每一列的最大值
apply(M, 2, max)
[1] 3 NA 9 12
#忽略NA值计算每列最大值
apply(M, 2, max, na.rm = TRUE)
[1] 3 6 9 12
*** rbind():根据行进行合并***
*** cbind():根据列进行合并***
# 添加一行,分别为每列平均值
M <- rbind(M, apply(M, 2, mean, na.rm = T))
M
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 NA 8 11
[3,] 3 6 9 12
[4,] 2 5 8 11 *** ***
# 添加行名
rownames(M) <- c(1:3, 'mean')
M
[,1] [,2] [,3] [,4]
1 1 4 7 10
2 2 NA 8 11
3 3 6 9 12
mean 2 5 8 11
# 添加一列,分别为每行的和
M <- cbind(M, apply(M, 1, sum, na.rm = T))
M
[,1] [,2] [,3] [,4] [,5]
1 1 4 7 10 22
2 2 NA 8 11 21
3 3 6 9 12 30
##mean 2 5 8 11 26
# 添加列名
colnames(M) <- c(1:4, 'sum')
M
1 2 3 4 sum
##1 1 4 7 10 22
##2 2 NA 8 11 21
##3 3 6 9 12 30
##mean 2 5 8 11 26
版权归原作者 m0_54356409 所有, 如有侵权,请联系我们删除。