5 min read

Matrices - Part 1

Introduction

In the previous post, we learnt to index/subset vectors. In this post, we will learn to create matrices. A matrix is a regular array of data elements, arranged in rows and columns. Matrices in R are homogeneous i.e. they can hold a single type of data. Matrix elements are indexed by specifying the row and column index and the elements of a matrix can filled either by row or column. In the first section, we look at various ways of creating matrices in R.

Creating Matrix

The easiest way to create a matrix in R is to use the matrix() function. Let us look at its syntax:

args(matrix)
## function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL) 
## NULL

Now that we have understood the syntax of the matrix() function, let us create a simple numeric matrix.

# matrix of 3 rows filled by columns
mat <- matrix(data = 1:9, nrow = 3, byrow = FALSE)
mat
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9

In the above example, we created a matrix of 3 rows where the data elements are filled by columns. We need to specify either the number of rows or columns and R will automatically compute the other. The number of data elements should be equal to the product of the rows and columns, else R will return a warning.

matrix(data = 1:9, nrow = 2, byrow = FALSE)
## Warning in matrix(data = 1:9, nrow = 2, byrow = FALSE): data length [9] is
## not a sub-multiple or multiple of the number of rows [2]
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    3    5    7    9
## [2,]    2    4    6    8    1
matrix(data = 1:10, nrow = 3, byrow = FALSE)
## Warning in matrix(data = 1:10, nrow = 3, byrow = FALSE): data length [10]
## is not a sub-multiple or multiple of the number of rows [3]
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8    1
## [3,]    3    6    9    2

We can follow some general rules to avoid the mistakes made in the previous examples:

  • if the number of elements is odd, both the number of rows and columns must be odd and their product should equal the number of data elements

  • if the number of elements is even, either the number of rows or columns must be even. In some cases, both the rows and columns must be even

Let us continue to explore the syntax of the matrix() function.

Fill Data by Row

matrix(data = 1:9, nrow = 3, byrow = TRUE)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9

Fill Data by Column

matrix(data = 1:9, nrow = 3, byrow = FALSE)
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9

Specify Rows

matrix(data = 1:10, nrow = 2)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    3    5    7    9
## [2,]    2    4    6    8   10

Specify Columns

matrix(data = 1:10, ncol = 5)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    3    5    7    9
## [2,]    2    4    6    8   10

Row & Column Names

You can specify names for the rows and columns of a matrix. To do so, we need to use list. Lists can contain other data structures such as vectors, matrices and even other lists. They are heterogeneous i.e. they can contain different data types. We will learn more about lists in a future post, for the time being let us learn how to create a basic list using the list() function:

# character vector
first_name <-   c("John", "Jill", "Jack")

# numeric vector
age <- c(20, 24, 32)

# list 
details <- list(first_name, age)
details
## [[1]]
## [1] "John" "Jill" "Jack"
## 
## [[2]]
## [1] 20 24 32

Now that we know how to create a list, let us go ahead and create a matrix and name its rows and columns.

# row names
row_names <- c('row_1', 'row_2', 'row_3')

# column names
col_names <- c('col_1', 'col_2', 'col_3')

# matrix with row and column names
matrix(data = 1:9, nrow = 3, dimnames = list(row_names, col_names))
##       col_1 col_2 col_3
## row_1     1     4     7
## row_2     2     5     8
## row_3     3     6     9

Matrix Dimension

Another useful function is dim(). It can be used to:

  • check the dimension (rows and columns) of a matrix
  • modify the dimension of a matrix
  • coerce a vector to a matrix

Check dimension of a matrix

mat <- matrix(data = 1:9, nrow = 3, byrow = TRUE)
dim(mat)
## [1] 3 3

Modify dimension of a matrix

# sample matrix
mat <- matrix(data = 1:12, nrow = 3, byrow = TRUE)
dim(mat)
## [1] 3 4
# change the dimension to 4 x 3
dim(mat) <- c(4, 3)
dim(mat)
## [1] 4 3

Coerce vector to matrix

# numeric vector
vect1 <- 1:12
vect1
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12
# coerce vect1 to a 4 x 3 matrix
dim(vect1) <- c(4, 3)
vect1
##      [,1] [,2] [,3]
## [1,]    1    5    9
## [2,]    2    6   10
## [3,]    3    7   11
## [4,]    4    8   12

Another way to coerce an R data structure to matrix is to use the as.matrix() function. Since the only other data structure we have learnt so far is the vector, we will coerce a vector into a matric using as.matrix(). We will deal with the other data structures as and when we learn them.

# numeric vector
vect1 <- 1:12
vect1
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12
# coerce vect1 to a matrix
as.matrix(vect1)
##       [,1]
##  [1,]    1
##  [2,]    2
##  [3,]    3
##  [4,]    4
##  [5,]    5
##  [6,]    6
##  [7,]    7
##  [8,]    8
##  [9,]    9
## [10,]   10
## [11,]   11
## [12,]   12

Regardless of the data type of the vector, all of them will be coerced to a matrix of dimension n x 1 i.e. they will all have only one column.