CodeInfoNet

Learn programming on the go!

Home » Matrices in R

Matrices in R

Matrices in R is a very interesting topic in R programming. In this article we are going to have a look at matrices in R and various operations that can be performed on them.

Also we will see how can we name the matrix, how to fetch the specific value from the matrix.

We are going to have a look at how to get the entire row or entire column. First let’s see what is matrix and how we can name them.

“So, the matrix in R is the combination of multiple vectors.” This is the most basic definition that I could ask for.

But, the basic difference between vector and a matrix is that vector has one dimension and matrix has two dimensions.

Let’s consider we have below sample matrix which we are going to create by using rbind() function later in this article.

Let’s call this matrix as A

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    6    7    8    9   10
[3,]   11   12   13   14   15
[4,]   16   17   18   19   20

What if you want to get the number 13 from the matrix A?

Similarly, what if I want to get the entire row or entire column from the matrix A? Is there any simple way available?

Because, In other programming languages, we might need to write a for loop for the entire row or entire column and fetch each element one by one.

But this is not the case in R programming. R provides very simple ways to access rows or columns or the single element from the entire matrix.

Let’s have a look at these ways:

Getting the specific element from matrix A

Consider we want to fetch the element 9 from matrix A.

What we have is the row number and the column number. These parameters are enough to get any element of matrix.

Hence,

A[2,4] will give me the expected element i.e 9.

We store it in another variable as:

Value <- A[2,4]

Getting the entire column from matrix A

Let’s say we need 4th column

A[,4] will give us the entire 4th column

Here we have specified just the column number.

And we already know that everything in a matrix is a vector. So we can easily store it in a variable as:

Column4 <- A[,4]

Getting the entire row from matrix A

Suppose we want to get the 2nd row from the matrix. As we have seen how to get the entire column just by specifying the column number, similarly specifying the row number gives us the entire row from matrices

Row2 <- A[2,]

Hence, we have seen the matrix, how to access the single element.

Also we have seen how to get the entire row or column.

Now, let’s have a look at how to create matrices by using multiple techniques.

  1. matrix() function
  2. rbind() function
  3. cbind() function

1. matrix() function

Although we do not use this function often to create vectors, but it is good to know the things.

matrix() function creates the vector in a simple way but it is not convenient. Let’s see why:

We create a data first to insert into the matrix.

myData <- 1:20		#creates a sequence of numbers 1-20

> myData
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

Now we will use matrix function to create a matrix from the above vector

myFirstMatrix <- matrix(myData, 4,5) 

Here, we have specified three parameters.

One is dataset, number of rows and number of columns that we want in a new matrix

Now let’s understand how it generates the matrix

> myFirstMatrix
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    5    9   13   17
[2,]    2    6   10   14   18
[3,]    3    7   11   15   19
[4,]    4    8   12   16   20

It generates the matrix in such a way that values are entered row by row till the specified number of rows reached, it again starts from the first row in next column.

In real life scenario, it may result in unwanted matrix. There is an option which can be used with the matrix() function.

The only difference it creates is it generates matrix by entering values column by column.

Here is the example:

myFirstMatrix <- matrix(myData, 4,5, byrow = True)

> myFirstMatrix
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    2    3    4    5
[2,]    6    7    8    9   10
[3,]   11   12   13   14   15
[4,]   16   17   18   19   20

Have a look at both the resulting matrices. The number of columns and the number of rows hasn’t changed at all.

But the order in which these matrices are bound is different.

We cannot rely on this function.

2. rbind() function

rbind() is a built in function of R. As a name suggests, it binds the rows in a matrix.

To be more specific, we can say that rbind() function binds the vectors row by row to form a matrix.

Let’s create our own matrix with the rbind() function.

First we need data to enter in our matrix so create it first

data1 <- c("Welcome","To", "www.codeinfo.net")
data2 <- c("Have a", "Great", "Day!!")
data3 <- c(1,10, 20)

So executing these lines will create vectors of type character and numerical.

rbindMatrix <- rbind(data1, data2, data3)

rbindMatrix

Output:
> rbindMatrix
      [,1]      [,2]    [,3]              
data1 "Welcome" "To"    "www.codeinfo.net"
data2 "Have a"  "Great" "Day!!"           
data3 "1"       "10"    "20"              

so we have created a matrix with rbind() function.

But you might be wondering that the value in data3 is numeric then why matrix showing them as a character?

The conclusion here is matrix doesn’t contain the data of different data types. It has considered the numbers as characters.

3. cbind() function

As we are aware that rbind() binds the rows so can you guess what cbind() will do?

Yes!! You are absolutely right.

It will bind the columns to create the matrix.

Let’s create another matrix with cbind() function

Here is the data first:

c1 <- 1:10
c2 <- 11:20

cbindMatrix <- cbind(c1,c2)
cbindMatrix

> cbindMatrix
      c1 c2
 [1,]  1 11
 [2,]  2 12
 [3,]  3 13
 [4,]  4 14
 [5,]  5 15
 [6,]  6 16
 [7,]  7 17
 [8,]  8 18
 [9,]  9 19
[10,] 10 20

So we have created a matrix by binding two columns.

Hence we have seen the matrix and multiple ways to create it.

Below is the one minute video of matrix operation we performed in this article.

The code is available for the series of articles present on www.codeinfo.net on Github.

Here is the Link: R programming Tutorials

Hence we know the basics of matrices in R. Keep reading for more articles on matrix operations.

Sumit Rajguru

Back to top