要从矩阵中删除行名或列名,我们只需要将它们设置为NULL,这样所有名称都将无效。例如,如果我们有一个包含行名和列名的矩阵M,则可以通过对列使用命令colnames(M)<-NULL和对行使用rownames(M)<-NULL来删除这些名称。
M1<-matrix(1:100,ncol=10) M1
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97 [8,] 8 18 28 38 48 58 68 78 88 98 [9,] 9 19 29 39 49 59 69 79 89 99 [10,] 10 20 30 40 50 60 70 80 90 100
colnames(M1)<-LETTERS[1:10] M1
输出结果
A B C D E F G H I J [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 1 4 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97 [8,] 8 18 28 38 48 58 68 78 88 98 [9,] 9 19 29 39 49 59 69 79 89 99 [10,] 10 20 30 40 50 60 70 80 90 100
colnames(M1)<-NULL M1
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97 [8,] 8 18 28 38 48 58 68 78 88 98 [9,] 9 19 29 39 49 59 69 79 89 99 [10,] 10 20 30 40 50 60 70 80 90 100
rownames(M1)<-letters[1:10] M1
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] a 1 11 21 31 41 51 61 71 81 91 b 2 12 22 32 42 52 62 72 82 92 c 3 13 23 33 43 53 63 73 83 93 d 4 14 24 34 44 54 64 74 84 94 e 5 15 25 35 45 55 65 75 85 95 f 6 16 26 36 46 56 66 76 86 96 g 7 17 27 37 47 57 67 77 87 97 h 8 18 28 38 48 58 68 78 88 98 i 9 19 29 39 49 59 69 79 89 99 j 10 20 30 40 50 60 70 80 90 100
rownames(M1)<-NULL M1
输出结果
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 11 21 31 41 51 61 71 81 91 [2,] 2 12 22 32 42 52 62 72 82 92 [3,] 3 13 23 33 43 53 63 73 83 93 [4,] 4 14 24 34 44 54 64 74 84 94 [5,] 5 15 25 35 45 55 65 75 85 95 [6,] 6 16 26 36 46 56 66 76 86 96 [7,] 7 17 27 37 47 57 67 77 87 97 [8,] 8 18 28 38 48 58 68 78 88 98 [9,] 9 19 29 39 49 59 69 79 89 99 [10,] 10 20 30 40 50 60 70 80 90 100
M2<-matrix(rnorm(50),nrow=10) M2
输出结果
[,1] [,2] [,3] [,4] [,5] [1,] 0.6802874 0.2144306 -1.4145804 0.62402960 -0.5938315 [2,] 0.4536187 0.5653905 0.6312822 2.56036083 0.7728485 [3,] 2.1096132 -0.7314662 0.3260058 -0.98057417 -0.3724911 [4,] -0.5953890 0.6637290 -1.6762956 -1.61804847 0.8662887 [5,] -0.7562208 -0.9417305 -1.7037116 0.60382315 -0.5236230 [6,] -1.2611174 -2.1865329 -0.2465629 -2.00149510 -1.0256434 [7,] 0.7827362 -0.8577172 1.1250864 -1.13954661 0.3187836 [8,] -0.8423010 -1.7105257 -0.2722357 -0.05741225 -0.9454204 [9,] -0.8249438 0.7572876 0.5679531 0.43816230 -1.5323482 [10,] 1.0914719 -0.9003930 0.7307768 -0.12886355 -1.5271211
colnames(M2)<-c("S1","S2","S3","S4","S5") M2
输出结果
S1 S2 S3 S4 S5 [1,] -0.21369037 -0.26807549 0.5780368 0.403231225 -0.3898034 [2,] -1.05153856 -0.91487726 -0.2038823 0.904563419 0.4286243 [3,] -0.10755935 0.36254026 -1.3386091 -0.218132138 0.3148615 [4,] 0.73045398 -0.09284451 -0.1476228 -1.196201762 1.3801728 [5,] 2.32148727 -1.51292926 1.7808169 -0.135465061 -0.8747653 [6,] 1.03602597 1.12718811 0.6376310 -0.009445365 -1.4074674 [7,] -0.05036301 -1.04252829 -0.8978156 -1.079780864 0.3735876 [8,] 1.01912253 1.28759140 0.1640043 -1.126854167 -0.2303916 [9,] 0.93337641 -1.24223632 0.6052778 -0.003241698 0.2684704 [10,] 0.30957289 -1.39528971 -0.7803917 0.946891126 -0.9375629
colnames(M2)<-NULL M2
输出结果
[,1] [,2] [,3] [,4] [,5] [1,] -0.21369037 -0.26807549 0.5780368 0.403231225 -0.3898034 [2,] -1.05153856 -0.91487726 -0.2038823 0.904563419 0.4286243 [3,] -0.10755935 0.36254026 -1.3386091 -0.218132138 0.3148615 [4,] 0.73045398 - 0.09284451 -0.1476228 -1.196201762 1.3801728 [5,] 2.32148727 -1.51292926 1.7808169 -0.135465061 -0.8747653 [6,] 1.03602597 1.12718811 0.6376310 -0.009445365 -1.4074674 [7,] -0.05036301 -1.04252829 -0.8978156 -1.079780864 0.3735876 [8,] 1.01912253 1.28759140 0.1640043 -1.126854167 - 0.2303916 [9,] 0.93337641 - 1.24223632 0.6052778 -0.003241698 0.2684704 [10,] 0.30957289 -1.39528971 -0.780391 0.946891126 -0.9375629
rownames(M2)<-c("R1","R2","R3","R4","R5","R6","R7","R8","R9","R10") M2
输出结果
[,1] [,2] [,3] [,4] [,5] R1 -0.21369037 - 0.26807549 0.5780368 0.403231225 -0.3898034 R2 -1.05153856 -0.91487726 -0.2038823 0.904563419 0.4286243 R3 -0.10755935 0.36254026 -1.3386091 -0.218132138 0.3148615 R4 0.73045398 -0.09284451 -0.1476228 -1.196201762 1.3801728 R5 2.32148727 -1.51292926 1.7808169 -0.135465061 -0.8747653 R6 1.03602597 1.12718811 0.6376310 -0.009445365 - 1.4074674 R7 -0.05036301 -1.04252829 -0.8978156 -1.079780864 0.3735876 R8 1.01912253 1.28759140 0.1640043 -1.126854167 -0.2303916 R9 0.93337641 -1.24223632 0.6052778 -0.003241698 0.2684704 R10 0.30957289 -1.39528971 -0.7803917 0.946891126 -0.9375629
rownames(M2)<-NULL M2
输出结果
[,1] [,2] [,3] [,4] [,5] [1,] -0.21369037 -0.26807549 0.5780368 0.403231225 -0.3898034 [2,] -1.05153856 -0.91487726 -0.2038823 0.904563419 0.4286243 [3,] -0.10755935 0.36254026 -1.3386091 -0.218132138 0.3148615 [4,] 0.73045398 -0.09284451 -0.1476228 - 1.196201762 1.3801728 [5,] 2.32148727 -1.51292926 1.7808169 -0.135465061 -0.8747653 [6,] 1.03602597 1.12718811 0.6376310 -0.009445365 -1.4074674 [7,] -0.05036301 -1.04252829 -0.8978156 - 1.079780864 0.3735876 [8,] 1.01912253 1.28759140 0.1640043 -1.126854167 -0.2303916 [9,] 0.93337641 -1.24223632 0.6052778 -0.003241698 0.2684704 [10,] 0.30957289 -1.39528971 -0.7803917 0.946891126 -0.9375629