- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove the row names or column names from a matrix in R?
To remove the row names or column names from a matrix, we just need to set them to NULL, in this way all the names will be nullified. For example, if we have a matrix M that contain row names and column names then we can remove those names by using the command colnames(M)<-NULL for columns and rownames(M)<-NULL for rows.
Example
M1<-matrix(1:100,ncol=10) M1
Output
[,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
Example
colnames(M1)<-LETTERS[1:10] M1
Output
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
Example
colnames(M1)<-NULL M1
Output
[,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
Example
rownames(M1)<-letters[1:10] M1
Output
[,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
Example
rownames(M1)<-NULL M1
Output
[,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
Example
M2<-matrix(rnorm(50),nrow=10) M2
Output
[,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
Example
colnames(M2)<-c("S1","S2","S3","S4","S5") M2
Output
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
Example
colnames(M2)<-NULL M2
Output
[,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
Example
rownames(M2)<-c("R1","R2","R3","R4","R5","R6","R7","R8","R9","R10") M2
Output
[,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
Example
rownames(M2)<-NULL M2
Output
[,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
- Related Articles
- How to find the column names and row names from a matrix in R?
- How to change the repeated row names and column names to a sequence in a matrix in R?
- How to convert a matrix into a data frame with column names and row names as variables in R?
- How to convert a matrix to a data frame with column names and row names as new columns in R?
- How to change the column names and row names of a data frame in R?
- How to remove column names from an R data frame?
- How to convert a row into column names in R?
- How to aggregate matrix columns by row names in R?
- How to remove multiple columns from matrix in R by using their names?
- How to remove a common suffix from column names in an R data frame?
- How to remove underscore from column names of an R data frame?
- How to get row index or column index based on their names in R?
- How to create a subset of a matrix in R using row names?
- How to remove names from a named vector in R?
- How to remove repeated column names in a data.table object in R?

Advertisements