Matrices are a very important concept in R programming, mostly used for statistics and data manipulation. Where vectors are a set of values stored across a single dimension, matrices are collections of stored values across multiple dimensions. Matrices can be simply thought of as a number of vectors that are saved and can be manipulated together. R has several powerful functions for this.

Table of Contents

## Creating Matrices in R Programming

The subscripting in matrices begins with 1. Therefore, [1,1] represents the element corresponding to the first-row first column, [1,2] represents the element at the first row and second column and so on.

Matrices in R are created in the following manner:

mymat <- matrix(c(2,4,6,8),ncol=2,nrow=2)

The first argument we pass to the matrix creation is a vector of values to populate the matrix with. Then we need to specify the no. of columns and rows, as indicated by the `ncol`

and `nrow`

arguments. The number of elements in the vector should always be equal to `ncol * nrow`

.

R stores matrices in a column first manner by default, i.e., it fills the first column and then moves on to the next column left to right. Let us display the **mymat** matrix we created above.

> mymat [,1] [,2] [1,] 2 6 [2,] 4 8

To fill the matrix in a row first manner, you need to provide an additional argument `byrow=TRUE`

to the above matrix creation function.

> mymat <- matrix(c(2,4,6,8),ncol=2,nrow=2,byrow=TRUE) > mymat [,1] [,2] [1,] 2 4 [2,] 6 8

You can also pre-create a matrix by specifying its size and populate it later accordingly.

#Create space for matrix > xmat <-matrix(ncol=3,nrow=3) #Populate elements of the matrix > xmat[1,1]=5 > xmat[1,2]=10 > xmat[1,3]=15 > xmat[2,1]=20 > xmat[2,2]=25 > xmat[2,3]=30 > xmat[3,1]=35 > xmat[3,2]=40 > xmat[3,3]=45 > xmat [,1] [,2] [,3] [1,] 5 10 15 [2,] 20 25 30 [3,] 35 40 45

It is also possible to assign names to columns and rows in a matrix. This will help document your datasets better and erases the confusion. The names to rows and columns in R can be supplied through a list using the `dimnames`

argument.

Suppose that we wish to create a matrix to hold the vital statistics of 3 boys, with columns age, height and weight. We need the rows to represent each boy’s name and columns to represent each of the vital stats. We can do that using the following code:

vitals <- matrix(c(7,90.8,45,8,75.5,37,10,100.1,50),ncol=3,nrow=3,byrow=TRUE, dimnames = list(c("Raj","Rahul","Ravi"),c("Age","Height","Weight")))

Let us now display the matrix.

> vitals Age Height Weight Raj 7 90.8 45 Rahul 8 75.5 37 Ravi 10 100.1 50

The names of the rows and columns can be retrieved using the `colnames()`

and `rownames()`

functions over the matrix object.

> colnames(vitals) [1] "Age" "Height" "Weight" > rownames(vitals) [1] "Raj" "Rahul" "Ravi"

It is also possible to reassign the row and column names using these functions. Suppose we need to change the column name Height and Weight to include some more info on the units in which they are measured, we can do that in the following manner:

> colnames(vitals) <- c("Age", "Height in cm", "Weight in KG") > vitals Age Height in cm Weight in KG Raj 7 90.8 45 Rahul 8 75.5 37 Ravi 10 100.1 50

## Operations on Matrices in R Programming

Matrices in R support basic linear algebra operations such as addition, multiplication (both element-wise and matrix multiplication) and multiplication by a scalar.

Matrix addition is very straightforward and needs both the operand matrices to be of the same dimension.

> x<-matrix(c(3,4,5,2,6,7,8,9,11),ncol=3,nrow=3) > y<-matrix(c(3,2,6,7,8,22,1,54,10),ncol=3,nrow=3) > x [,1] [,2] [,3] [1,] 3 2 8 [2,] 4 6 9 [3,] 5 7 11 > y [,1] [,2] [,3] [1,] 3 7 1 [2,] 2 8 54 [3,] 6 22 10 > x+y [,1] [,2] [,3] [1,] 6 9 9 [2,] 6 14 63 [3,] 11 29 21

For element-wise multiplication, we need both the matrices to have the same dimension, whereas, for matrix multiplication of an ** m x n** with a

**matrix,**

*p x q***and**

*n***should always be the same. Both examples are illustrated below.**

*p*Element-wise multiplication and multiplication by a scalar are specified using a `*`

symbol and matrix multiplication is specified using a `%*%`

symbol.

> x*y [,1] [,2] [,3] [1,] 9 14 8 [2,] 8 48 486 [3,] 30 154 110 > x*2 [,1] [,2] [,3] [1,] 6 4 16 [2,] 8 12 18 [3,] 10 14 22 > x%*%y [,1] [,2] [,3] [1,] 61 213 191 [2,] 78 274 418 [3,] 95 333 493

A few more advanced matrix operations will be covered in our next post on matrix algebra.

## Indexing Matrices in R Language

We have already seen the process of indexing vectors and extracting subsets in the previous post. Matrix indexing works in a similar fashion as illustrated below. Negative subscripts work for excluding a particular row or column.

#Defined matrix x > x [,1] [,2] [,3] [1,] 3 2 8 [2,] 4 6 9 [3,] 5 7 11 #Display only the first row > x[1,] [1] 3 2 8 #Display only the first column > x[,1] [1] 3 4 5 #Display the rows 1 to 2 > x[1:2,] [,1] [,2] [,3] [1,] 3 2 8 [2,] 4 6 9 #Display the columns 2 to 3 > x[,2:3] [,1] [,2] [1,] 2 8 [2,] 6 9 [3,] 7 11 #Display all rows except the second > x[-2,] [,1] [,2] [,3] [1,] 3 2 8 [2,] 5 7 11

You can also reassign these submatrices with new values in the following manner. Both columns 2 and 3 are changed to the specified new values.

> x[,2:3] <- matrix(c(2,2,2,2,2,2),ncol=2) > x [,1] [,2] [,3] [1,] 3 2 2 [2,] 4 2 2 [3,] 5 2 2