# Matrix in R!!!

Basics of Matrix

Matrix Manipulation

Few Other Operations

Accessing Matrix Elements

Quiz

### Basics of Matrix

Just to recap – Vectors are one dimensional collection of data which can store values only of one type.

Now extending the definition to matrix, we can say that matrix is a multidimensional data structure which can contain data of one data type only.

There are two basic component of a matrix i.e. Row and Column.

Let’s create a matrix to start with

```
> matrix(data=1:16, nrow=4, ncol=4)
```

```
## [,1] [,2] [,3] [,4]
## [1,] 1 5 9 13
## [2,] 2 6 10 14
## [3,] 3 7 11 15
## [4,] 4 8 12 16
```

In the call above we have passed three parameters:

- data = Which we want to use in order to create the matrix
- nrow = Number of rows in required matrix
- ncol = Number of columns in required matrix

If you observe, we are getting values filled by columns i.e. first 4 values went to the column 1 and so on.. what if we want it row wise?

```
> matrix(data=1:16, nrow=4, ncol=4, byrow = TRUE)
```

```
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 5 6 7 8
## [3,] 9 10 11 12
## [4,] 13 14 15 16
```

Just change the fourth parameter ‘byrow’ to TRUE and R will fill your matrix by row instead of by columns

Let see few more examples,

```
> matrix(data=1:32, nrow=4, ncol=4, byrow = TRUE)
```

```
## [,1] [,2] [,3] [,4]
## [1,] 1 2 3 4
## [2,] 5 6 7 8
## [3,] 9 10 11 12
## [4,] 13 14 15 16
```

```
> matrix(data=1:32, nrow=4, byrow = TRUE)
```

```
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 1 2 3 4 5 6 7 8
## [2,] 9 10 11 12 13 14 15 16
## [3,] 17 18 19 20 21 22 23 24
## [4,] 25 26 27 28 29 30 31 32
```

```
> matrix(data=1:8, byrow = TRUE)
```

```
## [,1]
## [1,] 1
## [2,] 2
## [3,] 3
## [4,] 4
## [5,] 5
## [6,] 6
## [7,] 7
## [8,] 8
```

```
> matrix(data=1:8)
```

```
## [,1]
## [1,] 1
## [2,] 2
## [3,] 3
## [4,] 4
## [5,] 5
## [6,] 6
## [7,] 7
## [8,] 8
```

### Matrix Manipulations

You can run manipulations over matrices, now we will see a few examples of the same

**Adding a number to matrix**

Let’s first create a matrix

```
> mat<-matrix(1,4,4)
> mat
```

```
## [,1] [,2] [,3] [,4]
## [1,] 1 1 1 1
## [2,] 1 1 1 1
## [3,] 1 1 1 1
## [4,] 1 1 1 1
```

Notice here that we have omitted the parameter names while calling matrix function. R allows to do it but you have to make sure that you are passing them in correct order

Now lets add 2 to the entire matrix

```
> mat2<-mat+2
> mat2
```

```
## [,1] [,2] [,3] [,4]
## [1,] 3 3 3 3
## [2,] 3 3 3 3
## [3,] 3 3 3 3
## [4,] 3 3 3 3
```

Let’s divide mat2 by 3 and see the result

```
> mat3<-mat2/3
> mat3
```

```
## [,1] [,2] [,3] [,4]
## [1,] 1 1 1 1
## [2,] 1 1 1 1
## [3,] 1 1 1 1
## [4,] 1 1 1 1
```

What if we add two matrices?

```
> mat4<-mat3+mat2
> mat4
```

```
## [,1] [,2] [,3] [,4]
## [1,] 4 4 4 4
## [2,] 4 4 4 4
## [3,] 4 4 4 4
## [4,] 4 4 4 4
```

### Few other operations

```
> sqrt(mat4)
```

```
## [,1] [,2] [,3] [,4]
## [1,] 2 2 2 2
## [2,] 2 2 2 2
## [3,] 2 2 2 2
## [4,] 2 2 2 2
```

Basically, all these operations are running on each element of the matrix.

There are few matrix based operations as well which you learnt in high school.

```
> mat5<-matrix(1:9,3,3)
> mat5
```

```
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
```

```
> mat5_transpose<-t(mat5)
> mat5_transpose
```

```
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
```

t() function is to transpose a matrix, i.e., converting rows to columns and vice versa.

Let’s multiply two matrices

```
> mat5*mat5_transpose
```

```
## [,1] [,2] [,3]
## [1,] 1 8 21
## [2,] 8 25 48
## [3,] 21 48 81
```

```
> mat5 %*% mat5_transpose
```

```
## [,1] [,2] [,3]
## [1,] 66 78 90
## [2,] 78 93 108
## [3,] 90 108 126
```

What is the difference between last two calls?

The first call does element by element multiplication while the second call corresponds to a matrix multiplication.

### Accessing Matrix Elements

Matrix elements can be accessed by a square operator i.e.,

matrix_name[row,column]

where matrix_name is the name of your matrix, row is the row index of the element you want to access and column is the column index of the element you want to access.

Let’s see what is stored at 1,1 position in mat5

```
> mat5[1,1]
```

```
## [1] 1
```

What will be the output of following piece of code?

```
> mat5[1,]
```

Remember, EVERYTHING BEFORE COMMA IS ROW AND EVERYTHING AFTER COMMA IS COLUMN.

So, the above line will return all the elements of row 1 for all the columns as we didn’t specify any thing after comma

```
## [1] 1 4 7
```

Same is true for columns as well-

```
## [1] 4 5 6
```

Try the following functions over mat5 and see the results

- dim()
- colSums()
- rowSums()

#Changing the matrix dimension after creation

It is possible to change the dimensions of a matrix after creation of it, for example,

Let’s create a matrix mat6 containing integers from 1 to 16 and having dimensions 4×4

```
> mat6<-matrix(1:16,4,4)
> mat6
```

```
## [,1] [,2] [,3] [,4]
## [1,] 1 5 9 13
## [2,] 2 6 10 14
## [3,] 3 7 11 15
## [4,] 4 8 12 16
```

Let see the dimensions of mat6

```
> dim(mat6)
```

```
## [1] 4 4
```

dim() function can not only be used to get dimensions but also to change it.

Now, assume we want mat6 dimensions to be 2×8 instead of 4×4.

This can be achieved by simply calling-

```
> dim(mat6)<-c(2,8)
> mat6
```

```
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] 1 3 5 7 9 11 13 15
## [2,] 2 4 6 8 10 12 14 16
```

Matrix are very powerful data types in R but you will rarely come across matrix and matrix operations in Data Science applications.

But most of the statistical methods use Matrices in the back-end, so it is good to understand matrix and how do they work.

### Quiz

You have gone through Matrix tutorial, let’s check your success!!

#### analyticsfreak

#### Latest posts by analyticsfreak (see all)

- Few interesting questions related to correlation - July 22, 2016
- How to make a reproducible example to share? - July 21, 2016
- Few random questions on Random Forest - July 20, 2016