### Matrices

In R Programming Matrix is a two dimensional data structure. They contain elements of the same atomic types. A Matrix can be created using the matrix() function. R can also be used for matrix calculations. Matrices have rows and columns containing a single data type. In a matrix, the order of rows and columns is important. Dimension can be checked directly with the dim() function and all attributes of an object can be checked with the attributes() function. Check the below example.

#### Creating a matrix in R:

m <- matrix(nrow = 2, ncol = 3)
dim(m)

attributes(m)
m <- matrix(1:20, nrow = 4, ncol = 5)
m

Output:

> dim(m)
[1] 2 3
>
> attributes(m)
\$dim
[1] 2 3

> m <- matrix(1:20, nrow = 4, ncol = 5)
> m
[,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

Matrices can also be created directly from vectors by adding a dimension attribute.

m <- 1:20
m
dim(m) <- c(4, 5)
m

Output:

m
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
> dim(m) <- c(4, 5)
> m
[,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

We can always check the data type of the variable using class() function.

class(m)

Output:

`[1] "matrix"`

Matrices can be created by column-binding or row-binding with the cbind() and rbind() functions.

x<-1:3
y<-10:12
z<-30:32
cbind(x,y,z)
rbind(x,y,z)

Output:

> cbind(x,y,z)
x y z
[1,] 1 10 30
[2,] 2 11 31
[3,] 3 12 32
> rbind(x,y,z)
[,1] [,2] [,3]
x 1 2 3
y 10 11 12
z 30 31 32

By default the matrix function reorders a vector into columns, but we can also tell R to use rows instead.

x <-1:9
matrix(x, nrow = 3, ncol = 3)
matrix(x, nrow = 3, ncol = 3, byrow = TRUE)

Output:

> cbind(x,y,z)
x y z
[1,] 1 10 30
[2,] 2 11 31
[3,] 3 12 32
> rbind(x,y,z)
[,1] [,2] [,3]
x 1 2 3
y 10 11 12
z 30 31 32

We can also create a matrix of a specified dimension where every element is the same.

z<- matrix(5, 3, 4)
z

Output:

> z
[,1] [,2] [,3] [,4]
[1,] 5 5 5 5
[2,] 5 5 5 5
[3,] 5 5 5 5

We can create a matrix with specified elements on the diagonal. (And 0 on the off-diagonals.)

diag(3)
diag(1:4)

Output:

> diag(3)
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
> diag(1:4)
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 2 0 0
[3,] 0 0 3 0
[4,] 0 0 0 4

It is possible to name the rows and columns of matrix during creation by passing a 2 elements list to the argument dimnames.

`x <- matrix(1:16, nrow = 4, dimnames = list(c("W","X","Y","Z"), c("A","B","C","D")))`

Output:

A B C D
W 1 5 9 13
X 2 6 10 14
Y 3 7 11 15
Z 4 8 12 16

There are two other useful functions colnames() and rownames() that can be used to access the rownames and colnames of a matrix or you can modify them as well by assigning new values.

`rownames(x)colnames(x)`

colnames(x) <- c(“C1″,”C2″,”C3″,”C4”)
rownames(x) <- c(“R1″,”R2″,”R3″,”R4”)

x

Output:

`> rownames(x)[1] "W" "X" "Y" "Z"> colnames(x)[1] "A" "B" "C" "D"`

> x
C1 C2 C3 C4
R1 1 5 9 13
R2 2 6 10 14
R3 3 7 11 15
R4 4 8 12 16

#### How to Extract/Access Elements From a Matrix?

Matrices can be subsetted using square brackets, []. However, since matrices are two dimensional, we need to specify both a row and a column when subsetting.

##### Using Integer as Index:

You could subset an entire row using below command

z<- matrix(1:12, 3, 4)

z[1, ]

Output:

[1]  1  4  7 10

We could also subset an entire column.

z[ ,2]

Output:

[1] 4 5 6

We can also use vectors to subset more than one row or column at a time. Here we subset to the first and third column of the second row.

z[2, c(1, 3)]

Output:

[1] 2 8

##### Using Character as Index:

`x <- matrix(1:16, nrow = 4, dimnames = list(c("W","X","Y","Z"), c("A","B","C","D")))xx[,"A"]x["W",]x[c("W","X"),c("A","B")]`

Output:

`xA B C DW 1 5 9 13X 2 6 10 14Y 3 7 11 15Z 4 8 12 16> x[,"A"]W X Y Z1 2 3 4> x["W",]A B C D1 5 9 13> x[c("W","X"),c("A","B")]A BW 1 5X 2 6`

##### Using Logical Vector as Index:

x[c(TRUE,FALSE,TRUE,FALSE),c(TRUE,TRUE,FALSE,FALSE)]

Output:

A B
W 1 5
Y 3 7

#### Matrix Operations:

x = 1:9
y = 9:1
x = matrix(x, 3, 3)
y = matrix(y, 3, 3)
x
x + y
x-y
x*y
x/y

Output:

> x
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> x + y
[,1] [,2] [,3]
[1,] 10 10 10
[2,] 10 10 10
[3,] 10 10 10
> x-y
[,1] [,2] [,3]
[1,] -8 -2 4
[2,] -6 0 6
[3,] -4 2 8
> x*y
[,1] [,2] [,3]
[1,] 9 24 21
[2,] 16 25 16
[3,] 21 24 9
> x/y
[,1] [,2] [,3]
[1,] 0.1111111 0.6666667 2.333333
[2,] 0.2500000 1.0000000 4.000000
[3,] 0.4285714 1.5000000 9.000000

Note that X * Y is not matrix multiplication. It is element by element multiplication. (Same for X / Y). Instead, matrix multiplication uses %*%. Other matrix functions include t() which gives the transpose of a matrix and solve() which returns the inverse of a square matrix if it is invertible.

x%*%y
t(x)

z<-matrix(c(9, 2, -3, 2, 4, -2, -3, -2, 16), 3, byrow = TRUE)
solve(z)

z<-matrix(c(9, 2, -3, 2, 4, -2, -3, -2, 16), 3, byrow = TRUE)
dim(z)
rowSums(z)
colSums(z)
rowMeans(z)
colMeans(z)
diag(z)

Output:

> x%*%y
[,1] [,2] [,3]
[1,] 90 54 18
[2,] 114 69 24
[3,] 138 84 30
> t(x)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
>
> z<-matrix(c(9, 2, -3, 2, 4, -2, -3, -2, 16), 3, byrow = TRUE)
> solve(z)
[,1] [,2] [,3]
[1,] 0.12931034 -0.05603448 0.01724138
[2,] -0.05603448 0.29094828 0.02586207
[3,] 0.01724138 0.02586207 0.06896552
> z<-matrix(c(9, 2, -3, 2, 4, -2, -3, -2, 16), 3, byrow = TRUE)
> dim(z)
[1] 3 3
> rowSums(z)
[1] 8 4 11
> colSums(z)
[1] 8 4 11
> rowMeans(z)
[1] 2.666667 1.333333 3.666667
> colMeans(z)
[1] 2.666667 1.333333 3.666667
> diag(z)
[1] 9 4 16

#### How to modify a matrix in R?

You can access any elements from matrix and then reassign the value to modify them like the below example.

`x <- matrix(1:16, nrow = 4, dimnames = list(c("W","X","Y","Z"), c("A","B","C","D")))x[2,2]<-100x`

Output:

A B C D
W 1 5 9 13
X 2 100 10 14
Y 3 7 11 15
Z 4 8 12 16

Below code will modify all elements less than 10.

x[x<10] <- 0

x

Output:

A B C D
W 0 0 0 13
X 0 100 10 14
Y 0 0 11 15
Z 0 0 12 16

You can add row or column using rbind() and cbind() function.

x<-rbind(x, c(100, 200, 300, 400)) #To add row
x<-cbind(x, c(1000, 2000, 3000, 4000)) #To add column
x

Output:

A B C D
W 0 0 0 13 1000
X 0 100 10 14 2000
Y 0 0 11 15 3000
Z 0 0 12 16 4000
100 200 300 400 1000

You can see that new added row and column has no names. It is blank only. So, if you want to give some name for the new added row and column then look at the code below.

`colnames(x)[5]<-"E"rownames(x)[5]<-"M"x`

Output:

A B C D E
W 0 0 0 13 1000
X 0 100 10 14 2000
Y 0 0 11 15 3000
Z 0 0 12 16 4000
M 100 200 300 400 1000