Matrix In R - Naming Column Using colnames() Function And Accessing Matrix Element By Column Name In R

In a previous article we have learned how to give the name of row and access element of matrix by name in R. Now we shall discuss and learn how to give a name of matrix column; i.e., naming of matrix dimension and access element by the name.

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.

R

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

> #Accessing R By Index
> MatrixOfTechnology[2, 3]
[1] "R"

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

R 

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.
  1. #Getting details of colnames() function  
  2. ?colnames()  
  3. help(colnames)  
R 

Once you print the matrix, it will look like below.

R 

The below code shows the syntax and how to use colnames() function in R to rename the column of a matrix.
  1. #Using colnames() function - To Rename Column  
  2. colnames(MatrixOfTechnology) <- c("First.C""Second.C""Third.C""Forth.C")  
Here, colnames() is a function name and MatrixOfTechnology is the matrix whose column we want to rename. Now, we shall print the matrix. It looks like as shown in the below image. As we can see clearly in the below image, the name of the column has been printed.
  1. #Printing Matrix  
  2. MatrixOfTechnology  
R

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

Case 1 - All Elements Of First Column

We can access all elements of the first column using by passing only first column name First.C as below.

R
  1. #All Elements Of First Column  
  2. MatrixOfTechnology[, "First.C"]  
Output

> MatrixOfTechnology[, "First.C"]
1.R 2.R 3.R
"C#" "JavaScript" "Power BI"

The above scenario will look like as shown in the below image:

R 

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

To access the elements of the second row, we shall pass as 2 for row and "Third.C" as the name of the third column instead of the index of the column.
  1. #Element Of Second Row And Third Column - Accessing R  
  2. MatrixOfTechnology[2, "Third.C"]  
Output

[1] "R"

R
 
It has been shown in the below image in R studio.

R 

Printing The Name Of Columns Of Matrix

Now we can print the name of columns using colnames() function and passing the name of matrix as shown below.
  1. #Using colnames() function - To print Column names  
  2. colnames(MatrixOfTechnology)  
Output

> colnames(MatrixOfTechnology)
[1] "First.C" "Second.C" "Third.C" "Forth.C"

R 

Summary

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

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.