- 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 find the column names and row names from a matrix in R?
The rownames and colnames functions are used to define the corresponding names of a matrix and if we want to extract those names then the same function will be used. For example, if we have a matrix called M that has row names and column names then these names can be found by using rownames(M) and colnames(M).
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
> rownames(M1)<-LETTERS[1:10] > colnames(M1)<-LETTERS[1:10] > M1
Output
A B C D E F G H I J 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
> Row_Names_M1<-rownames(M1) > Row_Names_M1
Output
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
Example
> Column_Names_M1<-colnames(M1) > Column_Names_M1
Output
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
Example
> M2<-matrix(rpois(25,2),nrow=5) > M2
Output
[,1] [,2] [,3] [,4] [,5] [1,] 2 1 4 0 2 [2,] 3 3 0 3 3 [3,] 0 4 3 4 1 [4,] 4 1 0 1 0 [5,] 0 4 2 4 1
Example
> rownames(M2)<-c("India","Russia","China","Canada","USA") > colnames(M2)<-c("India","Russia","China","Canada","USA") > M2
Output
India Russia China Canada USA India 2 1 4 0 2 Russia 3 3 0 3 3 China 0 4 3 4 1 Canada 4 1 0 1 0 USA 0 4 2 4 1
Example
> Row_Names_M2<-rownames(M2) > Row_Names_M2
Output
[1] "India" "Russia" "China" "Canada" "USA"
Example
Column_Names_M2<-colnames(M2) > Column_Names_M2
Output
[1] "India" "Russia" "China" "Canada" "USA"
Example
> M3<-matrix(rnorm(36),nrow=6) > M3
Output
[,1] [,2] [,3] [,4] [,5] [,6] [1,] -0.81749436 -0.32603647 0.03249139 1.5090542 -0.9081710 1.2238853 [2,] -0.04507115 1.07842603 -1.11499780 0.4417905 -0.2758092 -0.9476823 [3,] -1.03613764 -2.65759584 -1.15795880 -1.9723598 -0.8386178 -0.2994112 [4,] 0.10289330 -0.06578772 -0.11692342 0.6313891 0.9125360 -0.2126542 [5,] 0.28185191 1.13375235 -0.47490971 0.2593980 -0.9350917 -0.4375274 [6,] 0.08914057 -0.32701974 1.25754633 -1.7432336 1.1584573 0.4017967
Example
> rownames(M3)<-c("C1","C2","C3","C4","C5","C6") > colnames(M3)<-c("S1","S2","S3","S4","S5","S6") > M3
Output
S1 S2 S3 S4 S5 S6 C1 -0.81749436 -0.32603647 0.03249139 1.5090542 -0.9081710 1.2238853 C2 -0.04507115 1.07842603 -1.11499780 0.4417905 -0.2758092 -0.9476823 C3 -1.03613764 -2.65759584 -1.15795880 -1.9723598 -0.8386178 -0.2994112 C4 0.10289330 -0.06578772 -0.11692342 0.6313891 0.9125360 -0.2126542 C5 0.28185191 1.13375235 -0.47490971 0.2593980 -0.9350917 -0.4375274 C6 0.08914057 -0.32701974 1.25754633 -1.7432336 1.1584573 0.4017967
Example
> Row_Names_M3<-rownames(M3) > Row_Names_M3
Output
[1] "C1" "C2" "C3" "C4" "C5" "C6"
Example
> Column_Names_M3<-colnames(M3) > Column_Names_M3
Output
[1] "S1" "S2" "S3" "S4" "S5" "S6"
- Related Articles
- How to remove the row names or column 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 change the column names and row names of a data frame in R?
- How to convert a matrix to a data frame with column names and row names as new columns in R?
- How to convert a row into column names in R?
- How to aggregate matrix columns by row names in R?
- How to create a subset of a matrix in R using row names?
- Find the column and row names in the R data frame based on condition.
- How to sort an R data frame column without losing row names?
- How to convert a column values to column names in R?
- Define column and row names of a square matrix in a single line code if they are same in R.
- How to remove column names from an R data frame?
- How to get row index or column index based on their names in R?
- How to add suffix to column names in R?

Advertisements