# 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.

- #Creating Vectors
- Technology1 <- c("C#", "Java", "Cobol", ".Net")
- #Creating Matrix From Above Vector
- MatrixOfTechnology <- matrix(data=Technology1, nrow = 1, ncol = 4)
- #Adding Row To Matrix Using rbind() function
- MatrixOfTechnology <- rbind(MatrixOfTechnology, c("JavaScript", "NodeJs", "R", "Azure"))
- MatrixOfTechnology <- rbind(MatrixOfTechnology, c("Power BI", "ASP .Net", "Unity", "Block Chain"))
- #Printing Matrix
- MatrixOfTechnology
- #Accessing R By Index
- MatrixOfTechnology[2, 3]

**Output**

*> MatrixOfTechnology[2, 3]*

*[1] "R"*

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

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.

- #Getting details of rownames() function
- ?rownames()
- help(rownames)

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.

- #Printing Matrix
- 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"*

Below code shows the syntax and how to use rownames() function in R to rename the row of a matrix.

- #Using rownames() function
- rownames(MatrixOfTechnology) <- c("F.R", "S.R", "T.R")
- 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.

**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**

- #All Elements Of First Row
- 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.

- #Element R From 2nd Row And 3rd Column
- 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.

It has been shown in R studio as below.

**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.

- #All Elements of 3rd Column
- MatrixOfTechnology[, 3]

**Output**

*> MatrixOfTechnology[, 3]*

*F.R S.R T.R*

*"Cobol" "R" "Unity"*

It has been shown in R studio as below.

**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:

- #Printing Name of Rows Before Row Name
- rownames(MatrixOfTechnology)
- #Using rownames() function - To Rename Rows
- rownames(MatrixOfTechnology) <- c("F.R", "S.R", "T.R")
- #Printing Name of Rows After Row Name
- rownames(MatrixOfTechnology)

**Output**

*> rownames(MatrixOfTechnology)*

*NULL*

*> rownames(MatrixOfTechnology)*

*[1] "F.R" "S.R" "T.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.