Processing math: 100%

The following data array is used for problems 2.1–2.8

184183213115361287914

To help solve the problems, we’ll enter the array into R,

x <- matrix(c(1,3,5,8,8,2,3,7,4,13,6,9,18,11,12,14),4,4)

Double-Check to make sure we got the numbers in correctly.

x
##      [,1] [,2] [,3] [,4]
## [1,]    1    8    4   18
## [2,]    3    2   13   11
## [3,]    5    3    6   12
## [4,]    8    7    9   14

Looks good!

Next, we process the problems

2.1 Identify the following values:

  1. x22

We go down to the second row, and over to the second column, obtaining 2.

x[2,2]
## [1] 2
  1. x41 Down to the last row, stay in the first column, obtaining the number 8. We can verify this in R as
x[4,1]
## [1] 8
  1. x23 Similarly, we find 13
x[2,3]
## [1] 13

2.2 Compute 3i=12j=1xij. This sums all the elements in the first 3 rows and the first two columns. Processing the notation literally, we get

(1+8)+(3+2)+(5+3)=22

R does not quite implement summation notation directly. However, many simple summation operations can be translated into R, by first selecting the specific rows and columns to be summed, then using the sum function. In this case, we get sum(x[1:3,1:2]) which gives 22.

2.3 Compute ¯x3. This is the mean of the elements in the 3rd row, which is (5+3+6+12)/4=26/4=6.5. We can compute this in R in several ways. One way is to use the function `rowMeans’, which will give us all 4 row means simultaneously, and then select the 3rd element.

rowMeans(x)[3]
## [1] 6.5

Another approach is to extract the 3rd row, then divide its length by its sum. I’ll show you this in steps.

x[3,] ## extract all elements of 3rd row in a vector
## [1]  5  3  6 12
sum(x[3,])  ## sum of elements in 3rd row
## [1] 26
length(x[3,]) ## number of elements in 3rd row
## [1] 4
sum(x[3,]) / length(x[3,]) ## mean of elements in 3rd row
## [1] 6.5

2.4 Compute ¯x4 This is the mean of the elements in the 4th column. We can use the colMeans function directly as

colMeans(x)[4]
## [1] 13.75

Alternatively, we can compute it directly ourselves as

sum(x[,4])/length(x[,4])
## [1] 13.75

2.5 Compute x1 This is the sum of the elements in the first column. We can use the colSums function or do the calculation directly.

colSums(x)[1]
## [1] 17
sum(x[,1])
## [1] 17

2.6 Compute 4i=1x2i1 Here, we sum the squares of the 4 elements in the first column. This is a somewhat trickier use of R. In R, if x is a vector or matrix, x^2 and computes the squares of all the elements x simultaneously. After doing that, we can sum in the usual ways. I’ll do this in small steps so you can follow what happens:

x
##      [,1] [,2] [,3] [,4]
## [1,]    1    8    4   18
## [2,]    3    2   13   11
## [3,]    5    3    6   12
## [4,]    8    7    9   14
x^2
##      [,1] [,2] [,3] [,4]
## [1,]    1   64   16  324
## [2,]    9    4  169  121
## [3,]   25    9   36  144
## [4,]   64   49   81  196
colSums(x^2)
## [1]  99 126 302 785
colSums(x^2)[1]
## [1] 99
## alternatively
sum((x^2)[,1])
## [1] 99

2.7 Compute 3(4i=1xi2)2 This asks us to compute 3 times the square of the sum of the elements in the second column. We can write

3*(colSums(x)[2])^2
## [1] 1200

2.8 Compute (x2)2 Here we are asked (in the more simple dot notation) to square the second row sum.

(rowSums(x)[2])^2
## [1] 841

2.9 Write a summation expression, , that will sum the elements in boldface in the following array.

147258369

The question asks us to add the 7, 5, and 3. (Depending on your screen driver, the bold numbers may not show as boldface on your screen, but they will probably print correctly as boldface). These three numbers are x1,3, x2,2 and x3,1. Notice that the row subscript runs from 1 to 3, while the column suscript is always equal to 4 minus the row subscript. Consequently, we can write

3i=1xi,4i=x1,41+x2,42+x3,43=x1,3+x2,2+x3,1

The answer of course is 7+5+3=15.

One straightforward way of implementing this in R is to use a summing loop. See if you can figure out what is going on. I’ve added comments to help.

y <- matrix(c(1,2,3,4,5,6,7,8,9),3,3) ## input 3x3 data
total <- 0 ## zero out summing variable
for(i in 1:3){ ## for each value of i from 1 to 3
  total <- total + y[i,4-i] ## add y[i,4-i] to the current total
}
total ##indicate the current total
## [1] 15