R – Matrices

R Matrix is a two-dimensional representation of arrays. Matrix is created using Matrix () function.

Syntax:

Matrix (data, nrow, ncol, byrow, dimnames)

Where data — the data elements

nrow – number of rows

ncol – number of columns

byrow – fill matrix by rows

dimnames – name assigned to the rows and columns

Example:

> matrix (4:12, nrow=3, ncol=3)

[,1] [,2] [,3]

[1,] 4   7   10

[2,] 5   8   11

[3,] 6   9   12

> matrix (4:12, nrow=3)

[,1] [,2] [,3]

[1,] 4   7   10

[2,] 5   8   11

[3,] 6   9   12

> matrix (4:12, ncol=3)

[,1] [,2] [,3]

[1,] 4   7   10

[2,] 5   8   11

[3,] 6   9   12

By default, this matrix above is filled column-wise. To fill it row wise use byrow argument as below:

> matrix (4:12, ncol=3, byrow=TRUE)

[,1] [,2] [,3]

[1,] 4   5   6

[2,] 7   8   9

[3,] 10   11   12

Rows and column names can be set with argument dimnames as below:

> matrix (4:12, ncol=3, byrow=TRUE, dimnames=list(c(“ABA”,”TOBA”,”GOBBA”), c(“POY”,”TROY”,”COY”)))

POY TROY COY

ABA     4   5   6

TOBA   7   8   9

GOBBA 10   11 12

Class () function is used to check if a variable is a matrix or not. Attribute () function is used to check the attributes of objects

Example:

> x <- matrix(4:12, ncol=3, byrow=TRUE,dimnames=list(c(“ABA”,”TOBA”,”GOBBA”),c(“POY”,”TROY”,”COY”)))

> class(x)

[1] “matrix”

> attributes(x)

$dim

[1] 3 3

$dimnames

$dimnames[[1]]

[1] “ABA”   “TOBA” “GOBBA”

$dimnames[[2]]

[1] “POY” “TROY” “COY”

cbind() and rbind() functions are also used for creation of matrix as below:

> cbind(c(3,4,5),c(6,7,8), c(9,10,11))

[,1] [,2] [,3]

[1,]   3   6   9

[2,]   4   7   10

[3,]   5   8   11

> rbind(c(3,4,5),c(6,7,8), c(9,10,11))

[,1] [,2] [,3]

[1,]   3   4   5

[2,]   6   7  8

[3,]   9   10   11

dim() function used for setting dimension of a vector is also used for creating a matrix

> x <- c(“Boy”,”Toy”,”Koy”,”Loy”,”Roy”,”Moy”,”Joy”,”Goy”,”Foy”)

> dim(x) <- c(3,3)

> x

[,1] [,2] [,3]

[1,] “Boy” “Loy” “Joy”

[2,] “Toy” “Roy” “Goy”

[3,] “Koy” “Moy” “Foy”

Transpose of a Matrix:

Transpose of a matrix is the interchange of its rows and column using function t()

> y <- cbind(c(3,4,5),c(6,7,8), c(9,10,11))

> y

[,1] [,2] [,3]

[1,]   3   6   9

[2,]   4   7   10

[3,]   5   8   11

> t(y)

[,1] [,2] [,3]

[1,]   3   4   5

[2,]   6   7   8

[3,]   9   10   11




Access elements of a Matrix:

Elements of a matrix can be accessed by using row and column index of the element.

Print element of first column of second row.

> matrix(4:12, ncol=3, byrow=TRUE,dimnames=list(c(“ABA”,”TOBA”,”GOBBA”),c(“POY”,”TROY”,”COY”)))

POY TROY COY

ABA     4   5   6

TOBA   7   8   9

GOBBA 10   11 12

> x <- matrix(4:12, ncol=3, byrow=TRUE,dimnames=list(c(“ABA”,”TOBA”,”GOBBA”),c(“POY”,”TROY”,”COY”)))

> print(x[2,1])

[1] 7

Print element of third column of first row.

> print(x[1,3])

[1] 6

Print elements of third row

> print (x [3,])

POY TROY COY

10   11   12

Print elements of first column

> print(x[,1])

ABA TOBA GOBBA

4     7   10

Print elements of first and third column

> print (x [, c (1,3)])

POY COY

ABA     4   6

TOBA   7   9

GOBBA 10 12




Matrix Arithmetic:

Various mathematical operations are performed on Matrix using R operators: Addition, Subtraction, Multiplication, Division

> x <- rbind(c(3,4,5),c(6,7,8), c(9,10,11))

> y <- cbind(c(3,4,5),c(6,7,8), c(9,10,11))

> addition <- x + y

> print(addition)

[,1] [,2] [,3]

[1,]   6   10   14

[2,]   10   14   18

[3,]   14   18   22

> sub <- x – y

> print(sub)

[,1] [,2] [,3]

[1,]   0   -2   -4

[2,]   2   0   -2

[3,]  4   2   0

> mul <- x * y

> print(mul)

[,1] [,2] [,3]

[1,]   9   24   45

[2,]   24   49   80

[3,]   45   80 121

> div <- x/y

> print(div)

[,1]     [,2]     [,3]

[1,] 1.0 0.6666667 0.5555556

[2,] 1.5 1.0000000 0.8000000

[3,] 1.8 1.2500000 1.0000000