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



Join the conversation! Your thoughts help the community grow.