Matrix In R - How To Create Matrix In R

Introduction

R is an important programming language used heavily by statisticians. It is also used in machine learning, data science, research, and many more new fields. It is a computing environment where statistical data may be implemented. In this article, we shall discuss and learn about the packages in R.

In this article, we shall learn about one of the important concepts, that is, Matrix. We shall see how to create a matrix in R using R Studio.

Matrix

Matrix in R is a data element which is used to store the data in the form of rows and columns. It is a collection of data elements arranged in a two-dimensional rectangular format. A matrix can contain any values of any data types such as integer, character or boolean. One of the important point which we should always remember that a matrix can contain values of only the same basic data types. For example if we declare a matrix of integer then all elements of that matrix should only be an integer type.

The below image shows one of the basic forms of matrix of character type. It has two rows and three columns. The name of matrix is MatrixA. The elements of first column are C#, C while the elements of second column are Java, C++ and the values of third column are .Net and HTML. The elements of first row are C#, Java & .Net while the elements of second row are C, C++ and HTML.

R

So far, we have seen what is a matrix in R. Let's get started working with the matrix in R Studio.

Creating Matrix

Creating a matrix in R is very simple. We use function matrix() to create a matrix in R. Below example shows how to create a matrix in R. Here matrixA is the name of the matrix of data type integer. The element of the matrix is a vector of integer ranging from 1 to 9. There are 3 rows and 3 columns which have been declared by the parameter nrow and ncol of function matrix.
  1. #Creating Matrix  
  2. matrixA <- matrix(data = c(1:9), nrow = 3, ncol = 3)  
  3. matrixA  
Output

> matrixA
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9 

 

Datatype of Matrix

To know the datatype of the matrix, we use function typeof() as it has been used below. The same function is used to know the data type of other data elements in R.
  1. #Datatype of Matrix  
  2. typeof(matrixA)  
Output

> typeof(matrixA)
[1] "integer"

Transposition of Matrix

A matrix is said to be transposed if the number of rows becomes the number of colums and vice versa. The elements of rows and column in the case of transpose are interchanged with each other.

We use function function t() in lowercase to transpose a matrix as below
  1. #Matrix Transpose  
  2. matrixBAfterTranspose <- t(matrixA)  
  3. matrixBAfterTranspose  
Output

> matrixBAfterTranspose
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9

R.
 
Accessing Matrix Elements

We can access matrix elements by their index as well as their name. We use a square bracket [] to access the elments of matrix. The below example shows how to access elements of matrix.

Case 1 - First Element of First Row and First Column

Here we have to provide the values of row and column i.e., 1 for both. 
  1. #Accessing matrix elements  
  2. matrixBAfterTranspose[1, 1]  
Output

[1] 1

Case 2 - All Elements of First Row and all Column

Here only we have to provide the row values because all columns means the value of column while accessing is optional. So, in [1,] the value of row is 1 while the value of column is not provided as it is optional.
  1. #All Elements of First Row and all Column  
  2. matrixBAfterTranspose[1, ]  
Output

[1] 1 2 3 

Case 3 - All Elements of First Column and all Rows

Here we have provided 1 for accessing first column in [, 1]. 
  1. #All Elements of First Column and all Rows  
  2. matrixBAfterTranspose[, 1]  
Output
[1] 1 4 7 

Question

You are requested to write R code to access element 5 of matrixA in comment. It all depends upon your choice and you can skip this question as well. 

Summary

In this article, we have learned about matrix in R and we saw how to create a matrix in R. In the next article we shall learn more about matrix in R.

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.