函数: m <- matrix(1:20,nrow=4,ncol=5)
m2 <- matrix(1:20,4,5) 两种写法
表示数值从1到20 创建4行5列的矩阵
> x <-1:20
> x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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
> m2 <- matrix(1:20,4,5)
> m2
[,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
> m2 <- matrix(1:20,5,5)
> m2
[,1] [,2] [,3] [,4] [,5]
[1,] 1 6 11 16 1
[2,] 2 7 12 17 2
[3,] 3 8 13 18 3
[4,] 4 9 14 19 4
[5,] 5 10 15 20 5
> m2 <- matrix(1:20,4,6)#报错
Warning message:
In matrix(1:20, 4, 6) :
data length [20] is not a sub-multiple or multiple of the number of columns [6]
> m3 <- matrix(1:20,3,3)#报错
Warning message:
In matrix(1:20, 3, 3) :
data length [20] is not a sub-multiple or multiple of the number of rows [3]
#可以只写一个数 表示多少行列数会自动分配
> m4 <- matrix(1:20,4)
> m4
[,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
#byrow=T T表示前面给的数是固定行 F表示前面给的数固定列**
> m3 <- matrix(1:20,4,byrow=T)
> m3
[,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
rname,cnames分别创建了行和列的名字
dimnames(m)<-list(rname,cnames)函数给矩阵赋予一个名字
可以用名字访问改矩阵
数组(array)与矩阵类似,但是维度可以大于2。数组可通过array函数创建,形式如下:
myarray <- array(vector, dimensions, dimnames)
其中vector包含了数组中的数据,dimensions是一个数值型向量,给出了各个维度下标的最大值,而dimnames是可选的、各维度名称标签的列表。
也可用创建函数:dim()
下面代码构建了一个3维数组
> y <-1:20
> dim(y)<-c(2,2,5)
> y
, , 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
使用array创建带有标签名的数组
> dim1<- c("A1","A2","A3")
> dim2 <- c("B1","B2","B3")
> dim3<-c("C1","C2","C3","C4")
> z <- array(1:36, c(3, 3, 4), dimnames=list(dim1, dim2, dim3))
> z
, , C1
B1 B2 B3
A1 1 4 7
A2 2 5 8
A3 3 6 9
, , C2
B1 B2 B3
A1 10 13 16
A2 11 14 17
A3 12 15 18
, , C3
B1 B2 B3
A1 19 22 25
A2 20 23 26
A3 21 24 27
, , C4
B1 B2 B3
A1 28 31 34
A2 29 32 35
A3 30 33 36
矩阵可由向量转换得到,所以矩阵原则上用向量的索引方法
由于矩阵是管理二维数据的,所以使用两个下标便可以表示矩阵中的元素
矩阵索引与向量索引类似,也是使用下标和方括号来选择矩阵中的行、列或者元素
> 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[2,3]
[1] 8
#输出第一行
> m[1,]
[1] 1 2 3 4 5
输出第一行第二到四列
> m[1,c(2:4)]
[1] 2 3 4
输出第二行第三行第二到五列
> m[c(2,3),c(2:5)]
[,1] [,2] [,3] [,4]
[1,] 7 8 9 10
[2,] 12 13 14 15
第二行第一列的数字
> m[2]
[1] 6
去除掉第一行的第二列剩余的数字
> m[-1,2]
[1] 7 12 17
> m+1
[,1] [,2] [,3] [,4] [,5]
[1,] 2 3 4 5 6
[2,] 7 8 9 10 11
[3,] 12 13 14 15 16
[4,] 17 18 19 20 21
> 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] [,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
> n<-matrix(1:20,5,4,byrow=T)
> n
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
[4,] 13 14 15 16
[5,] 17 18 19 20
> m+n
Error in m + n : non-conformable arrays
每一行的和
> colSums(m)
[1] 34 38 42 46 50
每一行的平均值
> colMeans(m)
[1] 8.5 9.5 10.5 11.5 12.5
每一列的和
> rowSums(m)
[1] 15 40 65 90