Define column and row names of a square matrix in a single line code if they are same in R.


If we have a square matrix or we want to create a square matrix and the row names and column names for this matrix are same then we can define these names in a single line of code.

For Example, if we have a matrix called M that has 10 rows and 10 columns that has first ten alphabets as row and column names then we can define the column names as colnames(M)<-rownames(M)<-LETTERS[1:10].

Example 1

Consider the matrix given below −

M1<-matrix(rpois(25,5),ncol=5)
M1

The following dataframe is created

    [,1] [,2] [,3] [,4] [,5]
[1,]   3    5    7    6    6
[2,]   8    6    6    6    4
[3,]   2    6    2    8    7
[4,]   1    3    5    9    4
[5,]   3    6    6    7    3

To define the column names and row names of M1 on the above created data frame, add the following code to the above snippet −

M1<-matrix(rpois(25,5),ncol=5)
colnames(M1)<-rownames(M1)<-LETTERS[1:5]
M1

Output

If you execute all the above given snippets as a single program, it generates the following Output −

  A B C D E
A 3 5 7 6 6
B 8 6 6 6 4
C 2 6 2 8 7
D 1 3 5 9 4
E 3 6 6 7 3

Example 2

Consider the matrix given below −

M2<-matrix(rpois(25,50),ncol=5)
M2

The following dataframe is created

   [,1] [,2] [,3] [,4] [,5]
[1,] 54   40   53   42   51
[2,] 59   44   54   67   55
[3,] 51   42   54   47   55
[4,] 68   38   37   47   39
[5,] 35   52   53   58   51

To define the column names and row names of M2 on the above created data frame, add the following code to the above snippet −

M2<-matrix(rpois(25,50),ncol=5)
colnames(M2)<-rownames(M2)<-c("Asia","Americas","Africa","Europe","Oceania")
M2

Output

If you execute all the above given snippets as a single program, it generates the following Output −

       Asia Americas Africa Europe Oceania
Asia     54       40     53     42      51
Americas 59       44     54     67      55
Africa   51       42     54     47      55
Europe   68       38     37     47      39
Oceania  35       52     53     58      51

Example 3

Consider the following matrix

M3<-matrix(round(rnorm(25),2),ncol=5)
M3

The following dataframe is created

       [,1] [,2]  [,3]  [,4]  [,5]
[1,]  0.68 -0.35  0.76 -0.22  0.36
[2,] -0.42 -0.28 -0.63  0.09  1.22
[3,]  0.05 -0.91  1.21 -0.17  0.86
[4,]  0.99 -0.71 -0.20 -0.58 -0.02
[5,] -0.21 -1.43  1.45 -0.26 -1.51

To define the column names and row names of M3 on the above created data frame, add the following code to the above snippet −

M3<-matrix(round(rnorm(25),2),ncol=5)
colnames(M3)<-rownames(M3)<-c("Rate1","Rate2","Rate3","Rate4","Rate5")
M3

Output

If you execute all the above given snippets as a single program, it generates the following Output −

      Rate1 Rate2 Rate3 Rate4 Rate5
Rate1  0.68 -0.35  0.76 -0.22  0.36
Rate2 -0.42 -0.28 -0.63  0.09  1.22
Rate3  0.05 -0.91  1.21 -0.17  0.86
Rate4  0.99 -0.71 -0.20 -0.58 -0.02
Rate5 -0.21 -1.43  1.45 -0.26 -1.51

Example 4

Consider the matrix given below −

M4<-matrix(round(rnorm(25,500,),0),ncol=5)
M4

The following dataframe is created

    [,1] [,2] [,3] [,4] [,5]
[1,] 499  500  499  498  502
[2,] 501  501  501  501  499
[3,] 499  500  501  503  500
[4,] 501  502  500  500  501
[5,] 500  499  500  500  502

To define the column names and row names of M4 on the above created data frame, add the following code to the above snippet −

colnames(M4)<-rownames(M4)<-c("Score1","Score2","Score3","Score4","Score5")
M4

Output

If you execute all the above given snippets as a single program, it generates the following Output −

       Score1 Score2 Score3 Score4 Score5
Score1    499    500    499    498    502
Score2    501    501    501    501    499
Score3    499    500    501    503    500
Score4    501    502    500    500    501
Score5    500    499    500    500    502

Example 5

Consider the matrix given below −

M5<-matrix(rpois(25,100),ncol=5)
M5

The following dataframe is created

    [,1] [,2] [,3] [,4] [,5]
[1,]  92 116   107  100   91
[2,] 111  94   110   95   93
[3,]  87  98    91   88  107
[4,] 106  91    98   94  104
[5,]  97  94   108  104  102

To define the column names and row names of M5 on the above created data frame, add the following code to the above snippet −

M5<-matrix(rpois(25,100),ncol=5)
colnames(M5)<-rownames(M5)<-c("Sour","Salty","Meaty","Sweet","Bitter")
M5

Output

If you execute all the above given snippets as a single program, it generates the following Output −

     Sour Salty Meaty Sweet Bitter
Sour   92   116   107   100     91
Salty 111    94   110    95     93
Meaty  87    98    91    88    107
Sweet 106    91    98    94    104
Bitter 97    94   108   104    102

Updated on: 01-Nov-2021

262 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements