Chapter 3 - Matrices and Arrays

After gaining a solid foundation on vectors and likes from chapters 1 and 2, chapter 3 is a step higher that involves the formation of matrix with specified number of rows and columns as well as higher dimensional structures that are referred to as arrays.

Below are the exercises I attempted in chapter 3 EXERCISE 3.1

  1. Construct and store a 4*2 matrix that’s filled row-wise with the values 4.3,3.1,8.2,8.2,3.2,0.9,1.6,and 6.5,in that order.
mat <- matrix(c(4.3,3.1,8.2,8.2,3.2,0.9,1.6,6.5),nrow=4, ncol=2,byrow=TRUE)
  1. Confirm the dimensions of the matrix from (a) are 3*2 if you remove anyone row.
mat[-1,]
##      [,1] [,2]
## [1,]  8.2  8.2
## [2,]  3.2  0.9
## [3,]  1.6  6.5
dim(mat[-1,])
## [1] 3 2
  1. Overwrite the second column of the matrix from (a) with that same column sorted from smallest to largest.
mat[,2] <- sort(mat[,2], decreasing=FALSE)
mat
##      [,1] [,2]
## [1,]  4.3  0.9
## [2,]  8.2  3.1
## [3,]  3.2  6.5
## [4,]  1.6  8.2
  1. What does R return if you delete the fourth row and the first column from (c)? Use matrix to ensure the result is a single-column matrix,rather than a vector.
matrix(mat[-4,-1])
##      [,1]
## [1,]  0.9
## [2,]  3.1
## [3,]  6.5
  1. Store the bottom four elements of (c) as a new 2 * 2 matrix.
mat_b <- matrix(c(3.2,6.5,1.6,8.2), nrow=2,ncol=2, byrow=TRUE)
mat_b
##      [,1] [,2]
## [1,]  3.2  6.5
## [2,]  1.6  8.2
  1. Overwrite,in this order,the elements of (c) at positions (4,2),(1,2), (4,1),and(1,1) with (-1/2) of the two values on the diagonal of (e).
mat[c(4,1), c(2,1)] <- (-1/2 * diag(mat_b))
mat
##      [,1] [,2]
## [1,] -4.1 -4.1
## [2,]  8.2  3.1
## [3,]  3.2  6.5
## [4,] -1.6 -1.6

EXERCISE 3.2 a. Calculate the following:

A1 <- rbind(c(1,2),c(2,4),c(7,6))
A1
##      [,1] [,2]
## [1,]    1    2
## [2,]    2    4
## [3,]    7    6
A2 <- rbind(c(10,20),c(30,40),c(50,60))
A2
##      [,1] [,2]
## [1,]   10   20
## [2,]   30   40
## [3,]   50   60
2/7*(A1-A2)
##            [,1]       [,2]
## [1,]  -2.571429  -5.142857
## [2,]  -8.000000 -10.285714
## [3,] -12.285714 -15.428571
  1. Store these two matrices
A <- matrix(c(1,2,7))
A
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    7
B <- matrix(c(3,4,8))
B
##      [,1]
## [1,]    3
## [2,]    4
## [3,]    8

Which of the following multiplications are possible? For those that are, compute the result.

#i. A · B
#A%*%B
#This isn't possible because their dimensions are not compatible for multiplication. Two matrices A  and B  can be multiplied only if the number of columns of A equals the number of rows of B.

#ii. At · B
a <- t(A)
a%*%B
##      [,1]
## [1,]   67
#iii. Bt(A · At)
b <- t(B) 
b
##      [,1] [,2] [,3]
## [1,]    3    4    8
b%*%(A%*%a)
##      [,1] [,2] [,3]
## [1,]   67  134  469
#v. [(B · Bt) + (A · At) − 100I3]^-1
V  <- B%*%b 
V
##      [,1] [,2] [,3]
## [1,]    9   12   24
## [2,]   12   16   32
## [3,]   24   32   64
V2 <- A%*%a
V2
##      [,1] [,2] [,3]
## [1,]    1    2    7
## [2,]    2    4   14
## [3,]    7   14   49
VI <- 100*(diag(x=3))
VI
##      [,1] [,2] [,3]
## [1,]  100    0    0
## [2,]    0  100    0
## [3,]    0    0  100
solve((V+V2)-VI)
##              [,1]         [,2]        [,3]
## [1,] -0.007923676  0.003123274 0.007843334
## [2,]  0.003123274 -0.005350239 0.011483806
## [3,]  0.007843334  0.011483806 0.017584735
#c. confirm that A^−1 · A − I4 provides a 4 × 4 matrix of zeros.
C <- diag(c(2,3,5,-1)) 
C
##      [,1] [,2] [,3] [,4]
## [1,]    2    0    0    0
## [2,]    0    3    0    0
## [3,]    0    0    5    0
## [4,]    0    0    0   -1
C1 <- solve(C)
C1
##      [,1]      [,2] [,3] [,4]
## [1,]  0.5 0.0000000  0.0    0
## [2,]  0.0 0.3333333  0.0    0
## [3,]  0.0 0.0000000  0.2    0
## [4,]  0.0 0.0000000  0.0   -1
(C1%*%C)-(diag(x=4))
##      [,1] [,2] [,3] [,4]
## [1,]    0    0    0    0
## [2,]    0    0    0    0
## [3,]    0    0    0    0
## [4,]    0    0    0    0

EXERCISE 3.3 a. Create and store a three-dimensional array with six layers of a 4 × 2 matrix, filled with a decreasing sequence of values between 4.8 and 0.1 of the appropriate length.

AR <- array(data=4.8:0.1,dim = c(4,2,6))
AR
## , , 1
## 
##      [,1] [,2]
## [1,]  4.8  0.8
## [2,]  3.8  4.8
## [3,]  2.8  3.8
## [4,]  1.8  2.8
## 
## , , 2
## 
##      [,1] [,2]
## [1,]  1.8  2.8
## [2,]  0.8  1.8
## [3,]  4.8  0.8
## [4,]  3.8  4.8
## 
## , , 3
## 
##      [,1] [,2]
## [1,]  3.8  4.8
## [2,]  2.8  3.8
## [3,]  1.8  2.8
## [4,]  0.8  1.8
## 
## , , 4
## 
##      [,1] [,2]
## [1,]  0.8  1.8
## [2,]  4.8  0.8
## [3,]  3.8  4.8
## [4,]  2.8  3.8
## 
## , , 5
## 
##      [,1] [,2]
## [1,]  2.8  3.8
## [2,]  1.8  2.8
## [3,]  0.8  1.8
## [4,]  4.8  0.8
## 
## , , 6
## 
##      [,1] [,2]
## [1,]  4.8  0.8
## [2,]  3.8  4.8
## [3,]  2.8  3.8
## [4,]  1.8  2.8
  1. Extract and store as a new object the fourth- and first-row elements, in that order, of the second column only of all layers of (a).
BR <- AR[c(4,1),2,]
BR
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]  2.8  4.8  1.8  3.8  0.8  2.8
## [2,]  0.8  2.8  4.8  1.8  3.8  0.8
  1. Use a fourfold repetition of the second row of the matrix formed in (b) to fill a new array of dimensions 2 × 2 × 2 × 3.
CR <- array(data=rep(BR[2,],each=4),dim = c(2,2,2,3))
CR
## , , 1, 1
## 
##      [,1] [,2]
## [1,]  0.8  0.8
## [2,]  0.8  0.8
## 
## , , 2, 1
## 
##      [,1] [,2]
## [1,]  2.8  2.8
## [2,]  2.8  2.8
## 
## , , 1, 2
## 
##      [,1] [,2]
## [1,]  4.8  4.8
## [2,]  4.8  4.8
## 
## , , 2, 2
## 
##      [,1] [,2]
## [1,]  1.8  1.8
## [2,]  1.8  1.8
## 
## , , 1, 3
## 
##      [,1] [,2]
## [1,]  3.8  3.8
## [2,]  3.8  3.8
## 
## , , 2, 3
## 
##      [,1] [,2]
## [1,]  0.8  0.8
## [2,]  0.8  0.8
  1. Create a new array comprised of the results of deleting the sixth layer of (a).
DR <- array(data=4.8:0.1,dim = c(4,2,5))
DR
## , , 1
## 
##      [,1] [,2]
## [1,]  4.8  0.8
## [2,]  3.8  4.8
## [3,]  2.8  3.8
## [4,]  1.8  2.8
## 
## , , 2
## 
##      [,1] [,2]
## [1,]  1.8  2.8
## [2,]  0.8  1.8
## [3,]  4.8  0.8
## [4,]  3.8  4.8
## 
## , , 3
## 
##      [,1] [,2]
## [1,]  3.8  4.8
## [2,]  2.8  3.8
## [3,]  1.8  2.8
## [4,]  0.8  1.8
## 
## , , 4
## 
##      [,1] [,2]
## [1,]  0.8  1.8
## [2,]  4.8  0.8
## [3,]  3.8  4.8
## [4,]  2.8  3.8
## 
## , , 5
## 
##      [,1] [,2]
## [1,]  2.8  3.8
## [2,]  1.8  2.8
## [3,]  0.8  1.8
## [4,]  4.8  0.8
  1. Overwrite the second and fourth row elements of the second column of layers 1, 3, and 5 of (d) with −99.
DR[c(2,4),2,c(1,3,5)] <- -99
DR
## , , 1
## 
##      [,1]  [,2]
## [1,]  4.8   0.8
## [2,]  3.8 -99.0
## [3,]  2.8   3.8
## [4,]  1.8 -99.0
## 
## , , 2
## 
##      [,1] [,2]
## [1,]  1.8  2.8
## [2,]  0.8  1.8
## [3,]  4.8  0.8
## [4,]  3.8  4.8
## 
## , , 3
## 
##      [,1]  [,2]
## [1,]  3.8   4.8
## [2,]  2.8 -99.0
## [3,]  1.8   2.8
## [4,]  0.8 -99.0
## 
## , , 4
## 
##      [,1] [,2]
## [1,]  0.8  1.8
## [2,]  4.8  0.8
## [3,]  3.8  4.8
## [4,]  2.8  3.8
## 
## , , 5
## 
##      [,1]  [,2]
## [1,]  2.8   3.8
## [2,]  1.8 -99.0
## [3,]  0.8   1.8
## [4,]  4.8 -99.0

Chapter 4 - Non numeric values

Chapter 4 considered three important non-numeric data types: logicals, characters, and factors. These data types play an important role in effective use of R.

Below are the exercises I attempted in chapter 3 EXERCISE 4.1

  1. Store the following vector of 15 values as an object in your workspace: c(6,9,7,3,6,7,9,6,3,6,6,7,1,9,1). Identify the following elements
AA <- c(6,9,7,3,6,7,9,6,3,6,6,7,1,9,1)
#i. Those equal to 6
A1 <- AA==6
A1
##  [1]  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE
## [13] FALSE FALSE FALSE
#ii. Those greater than or equal to 6
A2 <- AA>=6
A2
##  [1]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
## [13] FALSE  TRUE FALSE
#iii. Those less than 6 + 2
A3 <- AA<(6+2)
A3
##  [1]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
## [13]  TRUE FALSE  TRUE
#iv. Those not equal to 6
A4 <- AA!=6
A4
##  [1] FALSE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE FALSE FALSE  TRUE
## [13]  TRUE  TRUE  TRUE
  1. Create a new vector from the one used in (a) by deleting its first three elements. With this new vector, fill a 2 × 2 × 3 array.Examine the array for the following entries:
B <- c(AA[-(1:3)])
B
##  [1] 3 6 7 9 6 3 6 6 7 1 9 1
BB <- array(data=B, dim = c(2,2,3))
BB
## , , 1
## 
##      [,1] [,2]
## [1,]    3    7
## [2,]    6    9
## 
## , , 2
## 
##      [,1] [,2]
## [1,]    6    6
## [2,]    3    6
## 
## , , 3
## 
##      [,1] [,2]
## [1,]    7    9
## [2,]    1    1
#i. Those less than or equal to 6 divided by 2, plus 4
B1 <- BB<=((6/2)+4)
B1
## , , 1
## 
##      [,1]  [,2]
## [1,] TRUE  TRUE
## [2,] TRUE FALSE
## 
## , , 2
## 
##      [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
## 
## , , 3
## 
##      [,1]  [,2]
## [1,] TRUE FALSE
## [2,] TRUE  TRUE
#ii. Those less than or equal to 6 divided by 2, plus 4, after
#increasing every element in the array by 2
B2 <- (BB+2)<=((6/2)+4)
B2
## , , 1
## 
##       [,1]  [,2]
## [1,]  TRUE FALSE
## [2,] FALSE FALSE
## 
## , , 2
## 
##       [,1]  [,2]
## [1,] FALSE FALSE
## [2,]  TRUE FALSE
## 
## , , 3
## 
##       [,1]  [,2]
## [1,] FALSE FALSE
## [2,]  TRUE  TRUE
  1. Confirm the specific locations of elements equal to 0 in the 10 × 10 identity matrix I10
C1 <- diag(10)
C1
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]    1    0    0    0    0    0    0    0    0     0
##  [2,]    0    1    0    0    0    0    0    0    0     0
##  [3,]    0    0    1    0    0    0    0    0    0     0
##  [4,]    0    0    0    1    0    0    0    0    0     0
##  [5,]    0    0    0    0    1    0    0    0    0     0
##  [6,]    0    0    0    0    0    1    0    0    0     0
##  [7,]    0    0    0    0    0    0    1    0    0     0
##  [8,]    0    0    0    0    0    0    0    1    0     0
##  [9,]    0    0    0    0    0    0    0    0    1     0
## [10,]    0    0    0    0    0    0    0    0    0     1
C2 <- C1==0
C2
##        [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
##  [1,] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [2,]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [3,]  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [4,]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [5,]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [6,]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE
##  [7,]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
##  [8,]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE
##  [9,]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
## [10,]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
  1. Check whether any of the values of the logical arrays created in (b) are TRUE. If they are, check whether they are all TRUE.
any(B2)
## [1] TRUE
any(B1)
## [1] TRUE
all(B2)
## [1] FALSE
all(B1)
## [1] FALSE
  1. By extracting the diagonal elements of the logical matrix created in (c), use any to confirm there are no TRUE entries.
E <- diag(C2)
E
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
any(E)
## [1] FALSE
all(E)
## [1] FALSE

EXERCISE 4.2 a. Store the vector c(7,1,7,10,5,9,10,3,10,8) as foo. Identify the elements greater than 5 OR equal to 2.

foo <- c(7,1,7,10,5,9,10,3,10,8)
A <- foo>5|foo ==2
A
##  [1]  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE
  1. Store the vector c(8,8,4,4,5,1,5,6,6,8) as bar. Identify the elements less than or equal to 6 AND not equal to 4.
bar <- c(8,8,4,4,5,1,5,6,6,8)
B <- bar<=6 & bar!=4
B
##  [1] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
  1. Identify the elements that satisfy (a) in foo AND satisfy (b) in bar.
A & B
##  [1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE FALSE
  1. Store a third vector called baz that is equal to the element-wise sum of foo and bar. Determine the following:
baz <- foo + bar
baz
##  [1] 15  9 11 14 10 10 15  9 16 16
  1. The elements of baz greater than or equal to 14 but not equal to 15
D <- baz>=14 & baz!=15
D
##  [1] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE
  1. The elements of the vector obtained via an element-wise division of baz by foo that are greater than 4 OR less than or equal to 2
D2 <- baz/foo
DD <- D2>4 | D2<=2
DD
##  [1] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE