Using Indices
You subset matrices with the [
operator.
To get the value at the second row and third column:
The row and column numbers are called indices.
Example
The sub-matrix consisting of elements in
- the second or fourth rows, and
- the first, second or third columns
can be gotten like this:
Example
The sub-matrix consisting of elements in
- the second or fourth rows, and
- any of the four columns
can be gotten like this:
numbersMat[c(2,4), ] # just leave "columns" part blank
A B C D
b 2 8 14 20
d 4 10 16 22
Example
The sub-matrix consisting of elements in
- any of the six rows and
- the first, second or third columns
can be gotten like this:
numbersMat[, 1:3] # just leave "rows" part blank
A B C
a 1 7 13
b 2 8 14
c 3 9 15
d 4 10 16
e 5 11 17
f 6 12 18
A (Slight) Exception
If you only want to pick from one row (or one column) then you don’t get a matrix back.
numbersMat[1, ] # just the first row
The result is a vector, not a matrix. (Usually that’s what you want.)
What If I WANT a Matrix Back?
Set the drop
parameter to FALSE
:
numbersMat[1, , drop = FALSE]
Caution: Watch Indices
Don’t ask for rows or columns that your matrix does not have:
numbersMat[2:3, 5] # oops! (only four columns)
Error in numbersMat[2:3, 5] : subscript out of bounds
Subsetting with Names …
… works just as with ordinary vectors:
numbersMat[c("a", "c"), LETTERS[2:3]]
Logical Subsetting …
… works just as with ordinary vectors:
rowNumbers <- 1:6
rowIsEven <- rowNumbers %% 2 == 0
rowIsEven
[1] FALSE TRUE FALSE TRUE FALSE TRUE
numbersMat[rowIsEven, ] # pick only the even-numbered rows
A B C D
b 2 8 14 20
d 4 10 16 22
f 6 12 18 24
Subsetting to Assign: I
numbersMat[2,3] <- 0
numbersMat
A B C D
a 1 7 13 19
b 2 8 0 20
c 3 9 15 21
d 4 10 16 22
e 5 11 17 23
f 6 12 18 24
Subsetting to Assign: II
You can assign to more than one place in a matrix:
numbersMat[2,] <- 0
numbersMat
A B C D
a 1 7 13 19
b 0 0 0 0
c 3 9 15 21
d 4 10 16 22
e 5 11 17 23
f 6 12 18 24
The 0
was recycled to make this happen!
Subsetting to Assign: III
You can assign different values to different places.
numbersMat[2,] <- c(100, 200, 300, 400)
numbersMat
A B C D
a 1 7 13 19
b 100 200 300 400
c 3 9 15 21
d 4 10 16 22
e 5 11 17 23
f 6 12 18 24
Arithmetic Operations
Here are two 2-by-2 matrices:
mat1 <- matrix(rep(1, 4), nrow = 2)
mat2 <- matrix(rep(2, 4), nrow = 2)
mat1
[,1] [,2]
[1,] 1 1
[2,] 1 1
[,1] [,2]
[1,] 2 2
[2,] 2 2
Addition
Matrices add element-wise:
[,1] [,2]
[1,] 3 3
[2,] 3 3
Subtraction, division, etc. all work the element-wise as well.
Recycling Applies
Suppose we have:
mat <- matrix(1:4, nrow = 2)
mat
[,1] [,2]
[1,] 1 3
[2,] 2 4
Multiply every element of mat
by 2:
[,1] [,2]
[1,] 2 6
[2,] 4 8
Recycling
This is the same as:
matrix(rep(2, 4), nrow = 2) * mat
Matrix Multiplication
*
does not do the matrix multiplication that we do in Linear Algebra.
To learn how R does “real” matrix multiplication, study the textbook.
Logical Operations
Boolean operations apply to matrices element-wise. The result is a matrix of logical values. Consider the original matrix numbersMat
:
numbersMat <- matrix(1:24, nrow = 6)
Determine which elements of numbersMat
are odd:
[,1] [,2] [,3] [,4]
[1,] TRUE TRUE TRUE TRUE
[2,] FALSE FALSE FALSE FALSE
[3,] TRUE TRUE TRUE TRUE
[4,] FALSE FALSE FALSE FALSE
[5,] TRUE TRUE TRUE TRUE
[6,] FALSE FALSE FALSE FALSE
Logical Subsetting
We can select elements from a matrix using a Boolean operator:
numbersMat[numbersMat %% 2 == 1]
[1] 1 3 5 7 9 11 13 15 17 19 21 23
The result is an ordinary, one-dimensional vector.