Programming in R - tutorial : Matrix Data Type

Author: Abhinav Agrawal

Matrix is a data type in R with the dimension attribute - the rows & the columns. It has the elements of same class type. We can have character, integer or complex elements in the matrices and so on. We cannot have elements of mixed modes/class types such as both integer and character elements in the same matrix.

Let us start with creating matrix


m1 <- 1:4  # m1 is a vector object with a sequence of elements 
dim(m1) <- c(2, 2)  # define the dimension as 2X2 (2 rows and 2 columns) to make it a matrix
m1  # prints the matrix
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
class(m1)  # show the class type, in this case it would be matrix class
## [1] "matrix"
typeof(m1)  # mode of the elements. In this case elements stored in the matrix are integer type 
## [1] "integer"
nrow(m1)  # outputs the number of rows in the matrix
## [1] 2
ncol(m1)  # outputs the number of columns in the matrix
## [1] 2

m2 <- matrix(nrow = 2, ncol = 2)  # we will get the matrix filled with NA as we have not defined the matrix values
m2
##      [,1] [,2]
## [1,]   NA   NA
## [2,]   NA   NA
dim(m2)
## [1] 2 2
attributes(m2)
## $dim
## [1] 2 2

m2 <- matrix(seq(1, 4, 1), nrow = 2, ncol = 2)  # used a seq() to get a sequence of elements
m2  # note that by default the matrix is filled column wise. To fill row wise, we need to tell R exclusively. 
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4

m2 <- matrix(seq(1, 4, 1), nrow = 2, ncol = 2, byrow = T)  # byrow = T would tell R to fill the elements row wise
m2
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4

matrix(c(1, 2, 3, 4), c(2, 2))  # elements and dimensions of the matrix both defined using a c()function
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4

m2 <- matrix(c(0 + (0+1i), 1 + (0+2i), 3 + (0+4i), 4 + (0+5i)), nrow = 2, ncol = 2, 
    byrow = T)
# byrow = T would tell R to fill the elements row wise
m2
##      [,1] [,2]
## [1,] 0+1i 1+2i
## [2,] 3+4i 4+5i
class(m2)
## [1] "matrix"
typeof(m2)
## [1] "complex"

m2 <- matrix(c("A", "B", "C", "D"), nrow = 2, ncol = 2, byrow = T)
# byrow = T would tell R to fill the elements row wise
m2
##      [,1] [,2]
## [1,] "A"  "B" 
## [2,] "C"  "D"
class(m2)
## [1] "matrix"
typeof(m2)
## [1] "character"

Now, let us do some subsetting in matrix

m3 <- matrix(1:9, nrow = 3, ncol = 3)
m3
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
m3[1, 1]  # extracting an element by giving its location
## [1] 1
m3[, 1]  # extracting row elements of a specific column, in this case column 1
## [1] 1 2 3
m3[, 2]  # extracting row elements of a specific column, in this case column 2
## [1] 4 5 6
m3[, 3]  # extracting row elements of a specific column, in this case column 3
## [1] 7 8 9
m3[1, ]  # extracting column elements of a specific row, in this case row 1
## [1] 1 4 7
m3[2, ]  # extracting column elements of a specific row, in this case row 2
## [1] 2 5 8
m3[3, ]  # extracting column elements of a specific row, in this case row 3
## [1] 3 6 9
diag(m3)  # extracts the diagonal elements of the matrix
## [1] 1 5 9

Assign row names and column names to the matrix

m4 <- matrix(1:4, byrow = T, ncol = 2)
m4
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4

colnames(m4) <- c("COL1", "COL2")
rownames(m4) <- c("ROW1", "ROW2")
m4
##      COL1 COL2
## ROW1    1    2
## ROW2    3    4

dimnames(m4) <- list(c("COL1", "COL2"), c("ROW1", "ROW2"))
m4
##      ROW1 ROW2
## COL1    1    2
## COL2    3    4

Using cbind() and rbind() functions in the matrix:

m6 <- 0:3
m7 <- 2:5
# both the vectors should be of either the same length or length of bigger
# vector should be the multiple of shorter length vector

cbind(m6, m7)  # fill column wise
##      m6 m7
## [1,]  0  2
## [2,]  1  3
## [3,]  2  4
## [4,]  3  5
rbind(m6, m7)  # fill row wise
##    [,1] [,2] [,3] [,4]
## m6    0    1    2    3
## m7    2    3    4    5

Mathematical operations on matrix

# create 2 matrices
mat10 <- matrix(5:8, c(2, 2))
mat11 <- matrix(seq(1, 8, 2), c(2, 2))

mat10 + 10  # Addition of scalar to a matrix
##      [,1] [,2]
## [1,]   15   17
## [2,]   16   18

mat10 + mat11  # Adding two matrices
##      [,1] [,2]
## [1,]    6   12
## [2,]    9   15

mat10 - mat11  # Substraction of 2 matrices
##      [,1] [,2]
## [1,]    4    2
## [2,]    3    1

mat10 * 2  # Matrix multiplication (scalar multiplied to a matrix)
##      [,1] [,2]
## [1,]   10   14
## [2,]   12   16

mat10 * mat11  # Multiplication of 2 matrices (element wise multiplication)
##      [,1] [,2]
## [1,]    5   35
## [2,]   18   56

mat10 %*% mat11  # Multiplication of matrix (the actual matrix multiplication)
##      [,1] [,2]
## [1,]   26   74
## [2,]   30   86

t(mat11)  # Transpose of a matrix
##      [,1] [,2]
## [1,]    1    3
## [2,]    5    7

solve(mat10)  # Inverse of a mtarix
##      [,1] [,2]
## [1,]   -4  3.5
## [2,]    3 -2.5

rowSums(mat10)  # sum of rows
## [1] 12 14

colSums(mat10)  # sum of columns
## [1] 11 15

colMeans(mat10)  # return the means of the columns 
## [1] 5.5 7.5

rowMeans(mat10)  # # return the means of the columns 
## [1] 6 7

sum(mat10)  # sum of all the elements in the matrix
## [1] 26

eigen(mat10)  # to calculate the eigen values
## $values
## [1] 13.1521 -0.1521
## 
## $vectors
##         [,1]    [,2]
## [1,] -0.6515 -0.8054
## [2,] -0.7587  0.5928

We may want to convert a vector to a matrix - using the as.matrix() function for exclusive coercion

mn <- 1:6
mn  # mn is a vector object
## [1] 1 2 3 4 5 6

as.matrix(mn)  # as.matrix() function converts the vector mn to a matrix object
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    3
## [4,]    4
## [5,]    5
## [6,]    6