R语言学习笔记-矩阵的创建与操作方法

一、创建矩阵

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 创建两个数字变量
var5=sample(31:60,25,replace=T)
var6=sample(1:30,25,replace=T)
# 创建两个5行5列的矩阵,创建5行5列的矩阵的原因,是为了方便说明问题;
A <- matrix(var5,nrow=5)
B <- matrix(var6,nrow=5)
> A
[,1] [,2] [,3] [,4] [,5]
[1,] 43 32 54 41 56
[2,] 56 45 37 57 38
[3,] 50 50 40 32 50
[4,] 34 56 49 36 42
[5,] 56 41 33 40 46
> B
[,1] [,2] [,3] [,4] [,5]
[1,] 18 21 23 1 18
[2,] 18 5 23 10 5
[3,] 26 6 26 8 26
[4,] 7 5 7 16 15
[5,] 29 20 1 15 2

二、操作矩阵

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# 结合矩阵A与B的行,A先B后;
C <- rbind(A,B)
# 结合矩阵A与B的行,B先A后;
D <- rbind(B,A)
# 结合矩阵A与B的列,A先B后;
E <- cbind(A,B)
# 结合矩阵A与B的列,B先A后;
F <- cbind(B,A)
> C
[,1] [,2] [,3] [,4] [,5]
[1,] 43 32 54 41 56
[2,] 56 45 37 57 38
[3,] 50 50 40 32 50
[4,] 34 56 49 36 42
[5,] 56 41 33 40 46
[6,] 18 21 23 1 18
[7,] 18 5 23 10 5
[8,] 26 6 26 8 26
[9,] 7 5 7 16 15
[10,] 29 20 1 15 2
> D
[,1] [,2] [,3] [,4] [,5]
[1,] 18 21 23 1 18
[2,] 18 5 23 10 5
[3,] 26 6 26 8 26
[4,] 7 5 7 16 15
[5,] 29 20 1 15 2
[6,] 43 32 54 41 56
[7,] 56 45 37 57 38
[8,] 50 50 40 32 50
[9,] 34 56 49 36 42
[10,] 56 41 33 40 46
> E
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 43 32 54 41 56 18 21 23 1 18
[2,] 56 45 37 57 38 18 5 23 10 5
[3,] 50 50 40 32 50 26 6 26 8 26
[4,] 34 56 49 36 42 7 5 7 16 15
[5,] 56 41 33 40 46 29 20 1 15 2
> F
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 18 21 23 1 18 43 32 54 41 56
[2,] 18 5 23 10 5 56 45 37 57 38
[3,] 26 6 26 8 26 50 50 40 32 50
[4,] 7 5 7 16 15 34 56 49 36 42
[5,] 29 20 1 15 2 56 41 33 40 46

# 选取矩阵A中的单个元素
> A[2,3]
[1] 37
# 选取整行;
> A[3,]
[1] 50 50 40 32 50
# 选取整列;
> A[,5]
[1] 56 38 50 42 46
# 选取多行,注意:生成的新矩阵行号改变;
> A[c(3,4),]
[,1] [,2] [,3] [,4] [,5]
[1,] 50 50 40 32 50
[2,] 34 56 49 36 42
# 选取多列,注意:生成的新矩阵列号改变;
> A[,c(1,3,5)]
[,1] [,2] [,3]
[1,] 43 54 56
[2,] 56 37 38
[3,] 50 40 50
[4,] 34 49 42
[5,] 56 33 46
# 更改矩阵B的行名与列名;
> dimnames(B)=list(c("R1","R2","R3","R4","R5"),c("C1","C2","C3","C4","C5"))
> B
C1 C2 C3 C4 C5
R1 18 21 23 1 18
R2 18 5 23 10 5
R3 26 6 26 8 26
R4 7 5 7 16 15
R5 29 20 1 15 2
# 对矩阵的行和列进行命名后,可以通过行名与列名操作矩阵;
# 选取单个元素;
> B["R4","C5"]
[1] 15
# 选取整行;
> B["R3",]
C1 C2 C3 C4 C5
26 6 26 8 26
# 选取整列;
> B[,"C2"]
R1 R2 R3 R4 R5
21 5 6 5 20
# 选取多行;
> B[c("R1","R2","R4"),]
C1 C2 C3 C4 C5
R1 18 21 23 1 18
R2 18 5 23 10 5
R4 7 5 7 16 15
# 选取多列;
> B[,c("C3","C5")]
C3 C5
R1 23 18
R2 23 5
R3 26 26
R4 7 15
R5 1 2
# 对矩阵C进行行列转置(transpose);
> G <- t(C)
> G
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 43 56 50 34 56 18 18 26 7 29
[2,] 32 45 50 56 41 21 5 6 5 20
[3,] 54 37 40 49 33 23 23 26 7 1
[4,] 41 57 32 36 40 1 10 8 16 15
[5,] 56 38 50 42 46 18 5 26 15 2
# 解构矩阵(Deconstruction);
>H <- c(D)
> H
[1] 18 18 26 7 29 43 56 50 34 56 21 5 6 5 20 32 45 50 56 41 23 23 26 7 1
[26] 54 37 40 49 33 1 10 8 16 15 41 57 32 36 40 18 5 26 15 2 56 38 50 42 46
# 解构之后就不是矩阵了;
> class(D)
[1] "matrix"
> class(H)
[1] "integer"
# 转换矩阵E为数据框;
I <- as.data.frame(E)
> I <- as.data.frame(E)
> I
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
1 43 32 54 41 56 18 21 23 1 18
2 56 45 37 57 38 18 5 23 10 5
3 50 50 40 32 50 26 6 26 8 26
4 34 56 49 36 42 7 5 7 16 15
5 56 41 33 40 46 29 20 1 15 2
# 先看看E的数据类型;
> class(E)
[1] "matrix"
> mode(E)
[1] "numeric"
> typeof(E)
[1] "integer"
# 再看一下I,转变成数据框;
> class(I)
[1] "data.frame"
> mode(I)
[1] "list"
> typeof(I)
[1] "list"
------ 本文结束 ------
坚持原创文章分享,您的支持将鼓励我继续创作!