# R语言初学者——数据结构之矩阵和数组（一）

### 矩阵

• 创建矩阵

mytrix=(vector,nrow=number_of_rows,ncol=number_of_columns,byrow=logical_value,dimnames=list(char_vector_rownames,char_vector_colnames))

``````> 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
#也可以如下简化
> n<-matrix(1:20,4,5)
> n
[,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(x,nrow=4,ncol=6)
Warning message:
In matrix(x, nrow = 4, ncol = 6) :
data length [20] is not a sub-multiple or multiple of the number of columns [6]

``````

还有这样的情况

``````> m<-matrix(x,nrow=4,ncol=4)
> m
[,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    6   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16
> m<-matrix(x,nrow=3,ncol=4)
Warning message:
In matrix(x, nrow = 3, ncol = 4) :
data length [20] is not a sub-multiple or multiple of the number of rows [3]``````

``````> m<-matrix(x,nrow=4,ncol=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``````

``````> rnames<-c('r1','r2','r3','r4')
> cnames<-c('c1','c2','c3','c4','c5')
> 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``````

``````> n<-matrix(x,nrow=4,ncol=5,byrow = T,dimnames = list(c('r1','r2','r3','r4'),c('c1','c2','c3','c4','c5')))
> n
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
> ``````

下面介绍一下dim()函数

``````> x
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
> 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
> ?dim
> dim(m)<-c(2,10)
> m
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1   11    2   12    3   13    4   14    5    15
[2,]    6   16    7   17    8   18    9   19   10    20
> ``````

dim(）会改变对象的维数，并且删除对象的行名称和列名称。

• 矩阵的索引

``````> 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[2,]#返回第2行
[1]  6  7  8  9 10
> m[,2]#返回第二列
[1]  2  7 12 17
> m[3]
[1] 11
> m[2]#如果不加逗号，返回的是某行的第一个元素。
[1] 6
> m[-1,2]#表示返回第二列除了第一个元素外的所有元素。
[1]  7 12 17
> m[-2,2]
[1]  2 12 17
> ``````

``````> dimnames(m) <- list(c('r1',"r2",'r3','r4'),c('c1','c2','c3','c4','c5'))
> 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``````
• 矩阵运算

``````> 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+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
> n=matrix(1:20,5,4,byrow=T)
> m+n
Error in m + n : non-conformable arrays``````

``````> m[1,]
c1 c2 c3 c4 c5
1  2  3  4  5
> t <- m[1,]
> sum(t)
[1] 15
> sum(m)#计算整个矩阵的和
[1] 210
> colSums(m)#计算列和
c1 c2 c3 c4 c5
34 38 42 46 50
> rowSums(m)#计算行和
r1 r2 r3 r4
15 40 65 90
> colMeans(m)
c1   c2   c3   c4   c5
8.5  9.5 10.5 11.5 12.5
> rowMeans(m)
r1 r2 r3 r4
3  8 13 18
``````

``````> colMeans(m)
c1   c2   c3   c4   c5
8.5  9.5 10.5 11.5 12.5
> rowMeans(m)
r1 r2 r3 r4
3  8 13 18
> n <- matrix(1:9,3,3)
> t <- matrix(2:10,3,3)
> n
[,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
> t
[,1] [,2] [,3]
[1,]    2    5    8
[2,]    3    6    9
[3,]    4    7   10
> 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(m)#取对角线
[1]  1  7 13 19
> diag(n)
[1] 1 5 9
> 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
> 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``````

;