7.2 Matrix Indexing
Matrices are incredibly useful in data analysis, but the primary reason we are talking about them now is to get you used to working in two dimensions. Let’s practice sub-setting with matrices.
We use the sub-setting operator [
to pick out parts of a matrix. For example, in order to get the element in the second row and third column of numbersMat
, ask for:
2,3] numbersMat[
## [1] 14
The row and column numbers are called indices.
If we want the entire second row, then we could ask for:
2,1:4] numbersMat[
## A B C D
## 2 8 14 20
The result is a one-dimensional vector consisting of the elements in the second row of numbersMat
. It inherits as its names the column names of numbersMat
.
Actually, if you want the entire row you don’t have to specify which columns you want. Just leave the spot after the comma empty, like this:
2, ] numbersMat[
## A B C D
## 2 8 14 20
What if you want some items on the second row, but only the items in columns 1, 2 and 4? Then frame your request in terms of a vector of column-indices:
2, c(1, 2, 4)] numbersMat[
## A B D
## 2 8 20
You can specify a vector of row-indices along with a vector of column-indices, if you like:
1:2, 1:3] numbersMat[
## A B C
## a 1 7 13
## b 2 8 14
If the vector has row or column names then you may use them in place of indices to make a selection:
c("B", "D")] numbersMat[,
## B D
## a 7 19
## b 8 20
## c 9 21
## d 10 22
## e 11 23
## f 12 24
You can use sub-setting to change the values of the elements of a matrix
2,3] <- 0
numbersMat[ 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
You can assign a value to an entire row:
2,] <- 0
numbersMat[ 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
In the code above, the 0 was “recycled” into each of the four elements of the second row
You can assign the elements of a vector to corresponding selected elements of a matrix:
2,] <- c(100, 200, 300, 400)
numbersMat[ 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
7.2.1 To Drop or Not?
Note that when we ask for a single row of numbersMat
we got a regular one-dimensional vector:
3, ] numbersMat[
## A B C D
## 3 9 15 21
The same things happens if we ask for a single column:
2] numbersMat[ ,
## a b c d e f
## 7 200 9 10 11 12
We get the second column of numbersMat
, but as a regular vector. It’s not a “column” anymore. (Note that it inherits the row names from numbersMat
.)
When a subset of a matrix comes from only one row or column, R takes the opportunity to “drop” the class of the subset from “matrix” to “vector.” If you would like the subset to stay a vector, set the drop
parameter, which by default is TRUE
, to FALSE
. Thus the second column of numbersMat
, kept as a matrix with six rows and one column, is found as follows:
2, drop = FALSE] numbersMat[ ,
## B
## a 7
## b 200
## c 9
## d 10
## e 11
## f 12
In most applications people want the simpler vector structure, so they usually leave drop
at its default value.
7.2.2 Practice Exercises
In these exercises we’ll work with the following matrix:
<- 1:40
numbers <- matrix(numbers, nrow = 4)
practiceMatrix rownames(practiceMatrix) <- letters[1:4]
colnames(practiceMatrix) <- LETTERS[1:10]
practiceMatrix
## A B C D E F G H I J
## a 1 5 9 13 17 21 25 29 33 37
## b 2 6 10 14 18 22 26 30 34 38
## c 3 7 11 15 19 23 27 31 35 39
## d 4 8 12 16 20 24 28 32 36 40
Write two different one-line commands to get this matrix:
## B C D E ## a 5 9 13 17 ## c 7 11 15 19
Write a one-line command to get this matrix:
## A C E G I ## a 1 9 17 25 33 ## b 2 10 18 26 34 ## c 3 11 19 27 35 ## d 4 12 20 28 36
Write a one-line command to get this vector:
## a b c d ## 1 2 3 4
Write a one-line command to get this vector:
## A B C D E F G H I J ## 2 6 10 14 18 22 26 30 34 38
Write a one-line command to get this matrix:
## A ## a 1 ## b 2 ## c 3 ## d 4
Write a convenient one-line command to get this matrix:
## A B C D E F G H I ## a 1 5 9 13 17 21 25 29 33 ## b 2 6 10 14 18 22 26 30 34 ## c 3 7 11 15 19 23 27 31 35 ## d 4 8 12 16 20 24 28 32 36
Write a convenient one-line command to get this matrix:
## A C D E F G H I ## a 1 9 13 17 21 25 29 33 ## b 2 10 14 18 22 26 30 34 ## c 3 11 15 19 23 27 31 35 ## d 4 12 16 20 24 28 32 36
Write a function called
myRowSums()
that will find the sums of the rows of any given matrix. The function should use afor
-loop (see the Chapter on Flow Control). The function should take a single parameter calledmat
, the matrix whose rows the user wishes to sum. It should work like this:<- matrix(1:24, ncol = 6) myMatrix myRowSums(mat = myMatrix)
## [1] 66 72 78 84
7.2.3 Solutions to Practice Exercises
Here are two ways:
c(1,3), 2:5] practiceMatrix[c("a","c"), 2:5] practiceMatrix[
Here’s one way:
seq(1, 9, by = 2)] practiceMatrix[ ,
Here’s one way:
1] practiceMatrix[ ,
Here’s one way:
2, ] practiceMatrix[
Here’s one way:
1, drop = FALSE] practiceMatrix[ ,
Here’s one way:
-10] practiceMatrix[ ,
Here’s one way:
-c(2, 10)] practiceMatrix[ ,
Here is one way to write the function:
<- function(mat) { myRowSums <- nrow(mat) n <- numeric(n) sums for (i in 1:n) { <- sum(mat[i, ]) sums[i] } sums }