In this lesson, we will introduce the following data structures in R.
Note: This lesson is based on the book: The Art of R Programming (Matloff (2011)).
The vector is the fundamental data type in R. In this session, We will focus on the first two topics below. The third topic will be introduced later this semester.
## [1] 85 21 99 38 217 304 47
## [1] 21 99 217 304 47
## [1] 7
## [1] 85 21 99 38 217 304
## [1] 7 3 9 11 2
## [1] 4 2 15
## [1] 0.25 3.00 0.80
## [1] 1 0 4
## [1] 7 8 9 10 11 12 13 14 15 16 17 18 19
## [1] 15 14 13 12 11 10 9 8 7 6
Be aware of operator precedence issues.
## [1] 0 1 2 3 4 5 6 7 8 9
## [1] 1 2 3 4 5 6 7 8 9
## [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
## [1] 0.100 0.325 0.550 0.775 1.000
## [1] 1 2 3 4 5 6 7 8 9 10
## [1] 1 3 5 7 9
Note: The order of arguments in default is from, to, and by.
## [1] NA NA NA
## [1] 3 3 3 3 3 3 3 3 3 3
## [1] 1 3 -2 1 3 -2 1 3 -2 1 3 -2 1 3 -2
## [1] 1 1 3 3 -2 -2
## [1] "Hello" "Hello" "Hello" "Hello"
## [1] TRUE
## [1] FALSE
## [1] NA
## [1] FALSE
Matrix is a two dimensional rectangular data structure in R. A matrix is a vector with two attributes: the number of rows and the number of columns.
Matrix row and column subscripts starts with 1. The matrix \(A\) below has \(m\) rows and \(n\) columns. We say that the dimension of \(A\) is \(m\times n\).
\[A = \left( \begin{array}{cccc} A[1,1] & A[1,2] & \ldots & A[1,n]\\ A[2,1] & A[2,2] & \ldots & A[2,n]\\ \vdots & \vdots & \ddots & \vdots\\ A[m,1] & A[m,2] & \ldots & A[m,n] \end{array} \right)\]
## [,1] [,2] [,3]
## [1,] 1 2 5
## [2,] 3 4 8
## [1] 2 3
## [1] 2
## [1] 3
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
## [3,] 5 8
## [,1] [,2]
## [1,] 7 15
## [2,] 10 22
## [,1] [,2]
## [1,] -2 -6
## [2,] -4 -8
## [,1] [,2]
## [1,] 2 6
## [2,] 4 8
set.seed(1000) # set a random seed
A <- matrix(sample(1:100, 12), ncol=3) # use 12 random numbers to create a 4 by 3 matrix.
A## [,1] [,2] [,3]
## [1,] 68 88 22
## [2,] 43 29 45
## [3,] 86 61 38
## [4,] 51 18 33
## [,1] [,2] [,3]
## [1,] 43 29 45
## [2,] 86 61 38
## [1] 88 29 61 18
We can also assign values to submatrices. The following code chunk shows an example based on A given on the previous slide.
## [,1] [,2] [,3]
## [1,] 68 88 22
## [2,] 43 29 45
## [3,] 86 61 38
## [4,] 51 18 33
## [,1] [,2] [,3]
## [1,] 1 7 22
## [2,] 2 5 45
## [3,] 3 8 38
## [4,] 4 -3 33
## [,1] [,2] [,3]
## [1,] 1 7 22
## [2,] 2 5 45
## [3,] 3 8 38
## [4,] 4 -3 33
## [,1] [,2] [,3]
## [1,] 1 7 22
## [2,] 3 8 38
## [,1] [,2] [,3]
## [1,] 1 7 22
## [2,] 3 8 38
A[,2] >= 6 finds rows in the 2nd column such that their values are at least 6 and returns a set of logical values. The which() function reports which indices are TRUE.
We should note that the filtering criterion can be based on a variable different from the one to which the filtering will be applied. This could be very useful when we have more than one matrices (or data frames) and we want to use the conditions from one matrix to extract values in another matrix.
## [,1] [,2] [,3]
## [1,] 1 7 22
## [2,] 2 5 45
## [3,] 3 8 38
## [4,] 4 -3 33
## [,1] [,2] [,3]
## [1,] 1 7 22
## [2,] 4 -3 33
Here, z %% 2 == 1 checks each element of z for being an odd number and returns a set of logical values.
Since matrices are vectors, we can apply vector operations to them as well. The code chunk below shows an example.
## [1] 1 2 3 4 5 6 7 8
## one
## [1,] 2 45 1
## [2,] 3 38 1
## [,1] [,2]
## 2 45
## 3 38
## one 1 1
## [,1] [,2] [,3]
## [1,] 1 2 5
## [2,] 3 4 8
## C1 C2 C3
## [1,] 1 2 5
## [2,] 3 4 8
## C1 C2 C3
## R1 1 2 5
## R2 3 4 8
# Create two matrices.
set.seed(1000)
matrix1 <- matrix(sample(1:100, 9), ncol=3)
matrix2 <- matrix(sample(1:100, 9), ncol=3)
# Take these matrices as input to the array.
result <- array(cbind(matrix1, matrix2), dim = c(3,3,2))
result## , , 1
##
## [,1] [,2] [,3]
## [1,] 68 51 61
## [2,] 43 88 18
## [3,] 86 29 22
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 45 41 58
## [2,] 38 29 55
## [3,] 33 26 18
You can utilize the following single character keyboard shortcuts to enable alternate display modes (Xie, Allaire, and Grolemund (2018)):
A: Switches show of current versus all slides (helpful for printing all pages)
B: Make fonts large
c: Show table of contents
S: Make fonts smaller