Matrix In R - Naming Row Using rownames() Function And Accessing Matrix Element By Row Name In R

Introduction

In this article, we will learn how to give a dimension name and access elements of the matrix by the name. As we know, matrix is a data element in R in the tabular form which has rows and column. We also know how to access elements of the matrix by element index or position. Now we shall learn and discuss how to give a name of matrix row; i.e., naming of matrix dimension and access element by the name.

Let's get started now.

Accessing the Matrix Element By Index

In the below matrix suppose we want to access element R by index then the answer will be MatrixOfTechnology[2, 3] where 2 is for 2nd row and 3 is for 3rd column. This matrix and code to acces R element has been shown in the below code. Here, we have created a vector of name Technology1, and passed it as data while creating matrix of name MatrixOfTechnology. After creating matrix we have used rbind() function for adding rows to the matrix.
  1. #Creating Vectors  
  2. Technology1 <- c("C#""Java""Cobol"".Net")  
  3. #Creating Matrix From Above Vector  
  4. MatrixOfTechnology <- matrix(data=Technology1, nrow = 1, ncol = 4)  
  5. #Adding Row To Matrix Using rbind() function  
  6. MatrixOfTechnology <- rbind(MatrixOfTechnology, c("JavaScript""NodeJs""R""Azure"))  
  7. MatrixOfTechnology <- rbind(MatrixOfTechnology, c("Power BI""ASP .Net""Unity""Block Chain"))  
  8. #Printing Matrix  
  9. MatrixOfTechnology  
  10. #Accessing R By Index  
  11. MatrixOfTechnology[2, 3]  
Output

> MatrixOfTechnology[2, 3]
[1] "R" 

R 

The above scenario has been shown by below image in R studio.

R

Now, to access matrix element by name we first learn how to give the name of matrix dimension in R using R studio.

Using rownames() function To Rename Matrix Row

We use rownames() function for renaming the matrix row in R. It is quite simple to use rownames() function. If you want to know more about rownames() function then you can get help about it in R studio using the command help(rownames) or ?rownames(). It will show a screen as shown below.
  1. #Getting details of rownames() function  
  2. ?rownames()  
  3. help(rownames) 
R

Now we shall rename the row of a matrix using rownames() function. We shall print the matrix before and after renaming rows. It looks like as below before renaming row of a matrix.
  1. #Printing Matrix  
  2. MatrixOfTechnology  
Output

> MatrixOfTechnology
[,1] [,2] [,3] [,4]
[1,] "C#" "Java" "Cobol" ".Net"
[2,] "JavaScript" "NodeJs" "R" "Azure"
[3,] "Power BI" "ASP .Net" "Unity" "Block Chain"

R 

Below code shows the syntax and how to use rownames() function in R to rename the row of a matrix.
  1. #Using rownames() function  
  2. rownames(MatrixOfTechnology) <- c("F.R""S.R""T.R")  
  3. MatrixOfTechnology  
Output

> MatrixOfTechnology
[,1] [,2] [,3] [,4]
F.R "C#" "Java" "Cobol" ".Net"
S.R "JavaScript" "NodeJs" "R" "Azure"
T.R "Power BI" "ASP .Net" "Unity" "Block Chain"

Here rowanames is the name of a function and we have provided matrixof name MatrixOfTechnology which we have to rename. The value of rownames() function is the vector of element "F.R", "S.R", "T.R". It corresponds the name of a first row, second row and third row of matrix named MatrixOfTechnology. Now once we shall print the matrix it will look like below with the name of a row as we had provided. Earlier the name of a row was not showing. It was showing just index 1, 2 and 3.

It has been shown in below image how it looks and works in R studio.

R 

Accessing Matrix Element After Using rownames() Function

We shall look at a different case of Sub-Setting i.e., accessing an element of the matrix element now. Although we can still access an element of the above matrix by an index of rows and columns we shall access with the row name what we had renamed above.

Case 1 - All Elements Of First Row

R 
  1. #All Elements Of First Row  
  2. MatrixOfTechnology["F.R", ]  
Output

> MatrixOfTechnology["F.R",]
[1] "C#" "Java" "Cobol" ".Net"

Case 2 - Element Of Second Row And Third Column - Accessing R

Now, suppose we have to access element R which is in the second row and third column.
  1. #Element R From 2nd Row And 3rd Column  
  2. MatrixOfTechnology["S.R", 3]  
Output

> MatrixOfTechnology["S.R", 3]
S.R
"R"

Description

Here, "S.R" is the name of the second row and 3 is for an index of the third column. As we have not renamed columns, we had just provided index number while accessing the element of that index.

R

It has been shown in R studio as below. 

R 

Case 3 - All Elements Of the Third Column

Here, we have to access all the elements of the third column so here row number or name is optional. So, the below code will give the desired output.
  1. #All Elements of 3rd Column  
  2. MatrixOfTechnology[, 3]  
Output

> MatrixOfTechnology[, 3]
F.R S.R T.R
"Cobol" "R" "Unity" 

R

It has been shown in R studio as below.

R 

Printing Name Of Rows Before And After Renaming Row

As we can see clearly in the below image, before renaming the row, the output of rownames (MatrixOfTechnology) function was NULL as it has not been named, while after providing row name using rownames(MatrixOfTechnology) <- c("F.R", "S.R", "T.R") the output of the function was:
  1. #Printing Name of Rows Before Row Name
  2. rownames(MatrixOfTechnology)  
  3. #Using rownames() function - To Rename Rows  
  4. rownames(MatrixOfTechnology) <- c("F.R""S.R""T.R")  
  5. #Printing Name of Rows After Row Name  
  6. rownames(MatrixOfTechnology)  
Output

> rownames(MatrixOfTechnology)
NULL
> rownames(MatrixOfTechnology)
[1] "F.R" "S.R" "T.R"

R 

Summary

In this article, we have learned how to use rownames() function to rename the rows of the matrix in R. We have also seen how to access elements of the matrix using a name of dimension i.e., from the row name. Apart from these, we have learned how to print the name of the row using function rownames().

I hope you have learned and enjoyed reading this article. You’re welcome to like, comment, share and leave any type of suggestion regarding this information.


Similar Articles