What is a matrix?

A matrix is a 2-dimensional array with \(m\) rows and \(n\) columns. All elements in a matrix have the same underlying type (numeric, character, logical, …).

Create a matrix in R

Use matrix():

matrix(data, nrow = 1, ncol = 1, byrow = FALSE)

Key arguments: - data: vector (or other object) used to fill the matrix - nrow: number of rows - ncol: number of columns - byrow: if FALSE (default) fill column-wise; if TRUE fill row-wise

Example: byrow = TRUE vs FALSE

matrix_a <- matrix(1:10, nrow = 5, ncol = 2, byrow = TRUE)
matrix_a
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4
## [3,]    5    6
## [4,]    7    8
## [5,]    9   10
dim(matrix_a)
## [1] 5 2
matrix_b <- matrix(1:10, nrow = 5, ncol = 2, byrow = FALSE)
matrix_b
##      [,1] [,2]
## [1,]    1    6
## [2,]    2    7
## [3,]    3    8
## [4,]    4    9
## [5,]    5   10
dim(matrix_b)
## [1] 5 2

Example: create a 4x3 matrix with ncol

matrix_c <- matrix(1:12, ncol = 3)
matrix_c
##      [,1] [,2] [,3]
## [1,]    1    5    9
## [2,]    2    6   10
## [3,]    3    7   11
## [4,]    4    8   12
dim(matrix_c)
## [1] 4 3

Add columns/rows (cbind / rbind)

cbind() binds objects by columns and rbind() binds objects by rows.

Add one column with cbind()

matrix_a1 <- cbind(matrix_a, 1:5)
matrix_a1
##      [,1] [,2] [,3]
## [1,]    1    2    1
## [2,]    3    4    2
## [3,]    5    6    3
## [4,]    7    8    4
## [5,]    9   10    5
dim(matrix_a1)
## [1] 5 3

Bind two matrices by columns

To bind matrices with cbind(), they must have the same number of rows.

left  <- matrix(1:12,  ncol = 3)   # 4x3
right <- matrix(13:24, ncol = 3)   # 4x3

matrix_d <- cbind(left, right)     # 4x6
matrix_d
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    5    9   13   17   21
## [2,]    2    6   10   14   18   22
## [3,]    3    7   11   15   19   23
## [4,]    4    8   12   16   20   24
dim(matrix_d)
## [1] 4 6

Add one row with rbind()

To bind matrices with rbind(), they must have the same number of columns.

matrix_c2 <- matrix(1:12, ncol = 3)  # 4x3
new_row <- c(1, 2, 3)

matrix_c2 <- rbind(matrix_c2, new_row)  # 5x3
matrix_c2
##         [,1] [,2] [,3]
##            1    5    9
##            2    6   10
##            3    7   11
##            4    8   12
## new_row    1    2    3
dim(matrix_c2)
## [1] 5 3

Slice a matrix

Use square brackets [row, col]. A comma separates row selection from column selection.

Examples: - m[1, 2] selects row 1, column 2. - m[1:3, 2:3] selects rows 1–3 and columns 2–3. - m[, 1] selects all rows in column 1. - m[1, ] selects all columns in row 1.

m <- matrix(1:12, ncol = 3)
m
##      [,1] [,2] [,3]
## [1,]    1    5    9
## [2,]    2    6   10
## [3,]    3    7   11
## [4,]    4    8   12
m
##      [,1] [,2] [,3]
## [1,]    1    5    9
## [2,]    2    6   10
## [3,]    3    7   11
## [4,]    4    8   12
m[1:3, 2:3]
##      [,1] [,2]
## [1,]    5    9
## [2,]    6   10
## [3,]    7   11
m[, 1]
## [1] 1 2 3 4
m[1, ]
## [1] 1 5 9

 

A work by Gianluca Sottile

gianluca.sottile@unipa.it