Introduction
In a previous article, we learned how to give the name of a row and access elements of the matrix by name in R. Now, we shall discuss and learn how to give a name to the matrix column; i.e., the naming of matrix dimension and accessing elements by the name.
Let's get started now. In the below matrix, we shall access R which is the element of the matrix by name. As we know by the index, it can be accessed by MatrixOfTechnology[2, 3] where 2 indicates the second row and 3 is for the third column.

Creating Matrix As Shown In The Above Image
The below R script creates a matrix as shown in the above image. This matrix has been created using vector and rbind() function. You can learn more about vector and rbind() function in my previous articles.
- #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
> #Accessing R By Index
> #Accessing R By Index
> MatrixOfTechnology[2, 3]
[1] "R"
The above scenario has been shown by the below image in R Studio.
Using colnames() function To Rename Matrix Column
We use colnames() function for renaming the matrix column in R. It is quite simple to use the colnames() function. If you want to know more about colnames() function, then you can get help about it in R Studio using the command help(colnames) or ?colnames(). It will show a screen as shown below.
The above scenario has been shown by the below image in R Studio.
Using colnames() function To Rename Matrix Column
We use colnames() function for renaming the matrix column in R. It is quite simple to use the colnames() function. If you want to know more about colnames() function, then you can get help about it in R Studio using the command help(colnames) or ?colnames(). It will show a screen as shown below.
- #Getting details of colnames() function
- ?colnames()
- help(colnames)
Once you print the matrix, it will look like below.
The below code shows the syntax and how to use colnames() function in R to rename the column of a matrix.




Join the conversation! Your thoughts help the community grow.