Chapter 2
Numbers, Arithmetic, Assignment and Vectors
Exercise 2.1
#a
a <- 2.3
(6*a+42)/3^(4.2-3.62)
## [1] 29.50556
#b. Which of the following squares negative 4 and adds 2 to the result?
(-4)^2+2
## [1] 18
-4^2+2
## [1] -14
(-4)^(2+2)
## [1] 256
-4^(2+2)
## [1] -256
#The correct answer is (-4)^2+2
#c.calculate the square root of half of the average of the following numbers#
mean(25.2,15,16.44,15.3,18.6)/2
## [1] 12.6
sqrt(12.6)
## [1] 3.549648
#d. Find loge0.3
log(0.3)
## [1] -1.203973
#e.compute the exponential transform of your answer to (d)
exp(-1.203973)
## [1] 0.2999999
#f.identify the representation of -0.00000000423546322
-0.00000000423546322
## [1] -4.235463e-09
Exercise 2.2
#a.Create an object that stores the value 3^2*4^1/8
obj <- 3^2*4^1/8
#b.overwrite your object in (a) by itself divided by 2.33. Print the result to the console
A <- obj/2.33
A
## [1] 1.93133
#c.Create a new object with the value -8.2*10^-13
B <- -8.2*10^-13
B
## [1] -8.2e-13
#d.Print directly to the console the result of multiplying (b) by (c)
A*B
## [1] -1.583691e-12
Exercise 2.3
#a.Create and store a sequence of values from 5 to -11 that progresses in steps of -0.3
bot <- seq(from=5,to=-11,by=-0.3)
bot
## [1] 5.0 4.7 4.4 4.1 3.8 3.5 3.2 2.9 2.6 2.3 2.0 1.7
## [13] 1.4 1.1 0.8 0.5 0.2 -0.1 -0.4 -0.7 -1.0 -1.3 -1.6 -1.9
## [25] -2.2 -2.5 -2.8 -3.1 -3.4 -3.7 -4.0 -4.3 -4.6 -4.9 -5.2 -5.5
## [37] -5.8 -6.1 -6.4 -6.7 -7.0 -7.3 -7.6 -7.9 -8.2 -8.5 -8.8 -9.1
## [49] -9.4 -9.7 -10.0 -10.3 -10.6 -10.9
#b.Overwrite the object from (a) using the same sequence with the order reversed
bot <- rev(bot)
bot
## [1] -10.9 -10.6 -10.3 -10.0 -9.7 -9.4 -9.1 -8.8 -8.5 -8.2 -7.9 -7.6
## [13] -7.3 -7.0 -6.7 -6.4 -6.1 -5.8 -5.5 -5.2 -4.9 -4.6 -4.3 -4.0
## [25] -3.7 -3.4 -3.1 -2.8 -2.5 -2.2 -1.9 -1.6 -1.3 -1.0 -0.7 -0.4
## [37] -0.1 0.2 0.5 0.8 1.1 1.4 1.7 2.0 2.3 2.6 2.9 3.2
## [49] 3.5 3.8 4.1 4.4 4.7 5.0
#c.repeat the vector c(-1,3,-5,7,-9)twice, with each element repeated 10 times,and store the result. Display the result stored from largest to smallest.
vec <-c(-1,3,-5,7,-9)
vec <- rep(vec,times=2,each=10)
vec
## [1] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3 3 3 3 3 3 3 3 3 3 -5 -5 -5 -5 -5
## [26] -5 -5 -5 -5 -5 7 7 7 7 7 7 7 7 7 7 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9
## [51] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3 3 3 3 3 3 3 3 3 3 -5 -5 -5 -5 -5
## [76] -5 -5 -5 -5 -5 7 7 7 7 7 7 7 7 7 7 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9
sort(vec,decreasing =TRUE)
## [1] 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 3 3 3 3 3
## [26] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
## [51] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5
## [76] -5 -5 -5 -5 -5 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9
length(vec)
## [1] 100
#d.Create and store a vector that contains, in any configuration, the following:
seq <- 6:12
rep <- rep(5.3,times=3)
neg <- -3
seq2 <- seq(from=102,to=100,length.out=9)
vec_2 <- c(seq,rep,neg,seq2)
vec_2
## [1] 6.00 7.00 8.00 9.00 10.00 11.00 12.00 5.30 5.30 5.30
## [11] -3.00 102.00 101.75 101.50 101.25 101.00 100.75 100.50 100.25 100.00
#e.Confirm that the length of the vector created in (d) is 20
length(vec_2)
## [1] 20
Exercise 2.4
#a.Create and store a vector that contains the following, in this order
seq_3 <- seq(from=3,to=6,length.out=5)
rep_2 <- rep(c(2,-5.1,-33),times=2)
val <- 7/42+2
bar <- c(seq_3,rep_2,val)
bar
## [1] 3.000000 3.750000 4.500000 5.250000 6.000000 2.000000
## [7] -5.100000 -33.000000 2.000000 -5.100000 -33.000000 2.166667
#b.Extract the first and last elements of (a) and store as new object
A <- c(bar[1],bar[length(bar)])
A
## [1] 3.000000 2.166667
#c.Store a third object by omitting first and last elements of a
bam <- bar[-c(1,length(bar))]
bam
## [1] 3.75 4.50 5.25 6.00 2.00 -5.10 -33.00 2.00 -5.10 -33.00
#d.Reconstruct a using only (b) and (c)
D <- c(A[1],bam,A[2])
D
## [1] 3.000000 3.750000 4.500000 5.250000 6.000000 2.000000
## [7] -5.100000 -33.000000 2.000000 -5.100000 -33.000000 2.166667
#e.Overwrite (a) with sorted values from smallest to largest
bar <- sort(bar,decreasing =FALSE)
bar
## [1] -33.000000 -33.000000 -5.100000 -5.100000 2.000000 2.000000
## [7] 2.166667 3.000000 3.750000 4.500000 5.250000 6.000000
#f.Use the colon operator as an index vector to reverse the order of (e)and confirm this is identical to using sort on (e) with decreasing=TRUE.
rev <- (bar[length(bar):1])
rev
## [1] 6.000000 5.250000 4.500000 3.750000 3.000000 2.166667
## [7] 2.000000 2.000000 -5.100000 -5.100000 -33.000000 -33.000000
sort(bar,decreasing =TRUE)
## [1] 6.000000 5.250000 4.500000 3.750000 3.000000 2.166667
## [7] 2.000000 2.000000 -5.100000 -5.100000 -33.000000 -33.000000
#g.Create a vector from (c) that repeats the third element of (c) three times, the sixth element four times, and the last element once.
foo <- c(rep(bam[3],times=3),rep(bam[6],times=4),bam[length(bam)])
foo
## [1] 5.25 5.25 5.25 -5.10 -5.10 -5.10 -5.10 -33.00
#h. Create a new vector as a copy of (e) by assigning (e) as is to a newly named object. Using this new copy of (e), overwrite the first, the fifth to the seventh (inclusive), and the last element with the values 99 to 95(inclusive),respectively.
bee <- bam
bee
## [1] 3.75 4.50 5.25 6.00 2.00 -5.10 -33.00 2.00 -5.10 -33.00
bee[c(1,5:7,length(bee))] <- 99:95
bee
## [1] 99.00 4.50 5.25 6.00 98.00 97.00 96.00 2.00 -5.10 95.00
Exercise 2.5
#a. Convert the vector c(2,0.5,1,2,0.5,1,2,0.5,1) to a vector of only 1s, using a vector of length 3
foo <- c(2,0.5,1,2,0.5,1,2,0.5,1)
pat <- c(2,0.5,1)
foo2 <- foo/rep(pat,3)
#b
F <- c(45, 77, 20, 19, 101, 120,212)
F
## [1] 45 77 20 19 101 120 212
C <- (5/9)*(F-32)
C
## [1] 7.222222 25.000000 -6.666667 -7.222222 38.333333 48.888889 100.000000
#c.Use the vector c(2,4,6) and the vector c(1,2) in conjunction with rep and * to produce the vector c(2,4,6,4,8,12)
x <- c(2,4,6)
y <- c(1,2)
xy <- (rep(y,each=length(x))*rep(x,times=length(y)))
xy
## [1] 2 4 6 4 8 12
#d. Overwrite the middle four elements of the resulting vector from(c) with the two recycled values-0.1 and-100, in that order
vec <- xy
vec[2:5] <- c(-0.1,-100)
vec
## [1] 2.0 -0.1 -100.0 -0.1 -100.0 12.0
Chapter 3
Matrices and Arrays
Exercise 3.1
#a. Construct and store a 4x2 matrix that’s filled row-wise with the values 4.3,3.1,8.2,8.2,3.2,0.9,1.6,and6.5,in that order
foo <- matrix(data=c(4.3,3.1,8.2,8.2,3.2,0.9,1.6,6.5),nrow=4,ncol=2,byrow=TRUE)
foo
## [,1] [,2]
## [1,] 4.3 3.1
## [2,] 8.2 8.2
## [3,] 3.2 0.9
## [4,] 1.6 6.5
#b.Confirm the dimensions of the matrix from (a) are 3x2 if you remove any one row.
b <- foo[-2,]
b
## [,1] [,2]
## [1,] 4.3 3.1
## [2,] 3.2 0.9
## [3,] 1.6 6.5
dim(b)
## [1] 3 2
#c.Overwrite the second column of the matrix from (a) with that same column sorted from smallest to largest.
foo[,2] <- sort(foo[,2],decreasing = FALSE)
foo
## [,1] [,2]
## [1,] 4.3 0.9
## [2,] 8.2 3.1
## [3,] 3.2 6.5
## [4,] 1.6 8.2
#d.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
bar <- foo[-4,-1]
matrix(data=bar)
## [,1]
## [1,] 0.9
## [2,] 3.1
## [3,] 6.5
#e.Store the bottom four elements of (c) as a new 2x2 matrix
bee <- matrix(data=c(3.2,1.6,6.5,8.2),nrow=2,ncol=2)
bee
## [,1] [,2]
## [1,] 3.2 6.5
## [2,] 1.6 8.2
Exercise 3.2
#a. Calculate the following
a <- 2/7
A <- cbind(c(1,2,7),c(2,4,6))
A
## [,1] [,2]
## [1,] 1 2
## [2,] 2 4
## [3,] 7 6
B <- cbind(c(10,30,50),c(20,40,60))
B
## [,1] [,2]
## [1,] 10 20
## [2,] 30 40
## [3,] 50 60
a*(A-B)
## [,1] [,2]
## [1,] -2.571429 -5.142857
## [2,] -8.000000 -10.285714
## [3,] -12.285714 -15.428571
#b. store these two matrices:
A <- cbind(c(1,2,7))
A
## [,1]
## [1,] 1
## [2,] 2
## [3,] 7
B <- cbind(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
#b(i) A.B not possible
#b(ii)
t(A)%*%B
## [,1]
## [1,] 67
#b(iii)
t(B)%*%(A%*%t(A))
## [,1] [,2] [,3]
## [1,] 67 134 469
#b (iv) gives error
#b(v)
x <- B%*%t(B)
y<- A%*%t(A)
z<- (100*diag(x=3))
z
## [,1] [,2] [,3]
## [1,] 100 0 0
## [2,] 0 100 0
## [3,] 0 0 100
x+y
## [,1] [,2] [,3]
## [1,] 10 14 31
## [2,] 14 20 46
## [3,] 31 46 113
diag(x+y)
## [1] 10 20 113
solve(x+y-z)
## [,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
A <- diag(c(2,3,5,-1))
A
## [,1] [,2] [,3] [,4]
## [1,] 2 0 0 0
## [2,] 0 3 0 0
## [3,] 0 0 5 0
## [4,] 0 0 0 -1
# confirm that A^1.A-I4 provides a 4x4 matrix of zeros
dim(c)
## NULL
solve(A)%*%A-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 4x2 matrix,filled with a decreasing sequence of values between 4.8 and 0.1 of the appropriate length
foo <- seq(from=4.8,to=0.1,length.out=48)
foo
## [1] 4.8 4.7 4.6 4.5 4.4 4.3 4.2 4.1 4.0 3.9 3.8 3.7 3.6 3.5 3.4 3.3 3.2 3.1 3.0
## [20] 2.9 2.8 2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0 1.9 1.8 1.7 1.6 1.5 1.4 1.3 1.2 1.1
## [39] 1.0 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1
foo <- rev(foo)
foo
## [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
## [20] 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8
## [39] 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8
bar <- array(foo,dim = c(4,2,6))
bar
## , , 1
##
## [,1] [,2]
## [1,] 0.1 0.5
## [2,] 0.2 0.6
## [3,] 0.3 0.7
## [4,] 0.4 0.8
##
## , , 2
##
## [,1] [,2]
## [1,] 0.9 1.3
## [2,] 1.0 1.4
## [3,] 1.1 1.5
## [4,] 1.2 1.6
##
## , , 3
##
## [,1] [,2]
## [1,] 1.7 2.1
## [2,] 1.8 2.2
## [3,] 1.9 2.3
## [4,] 2.0 2.4
##
## , , 4
##
## [,1] [,2]
## [1,] 2.5 2.9
## [2,] 2.6 3.0
## [3,] 2.7 3.1
## [4,] 2.8 3.2
##
## , , 5
##
## [,1] [,2]
## [1,] 3.3 3.7
## [2,] 3.4 3.8
## [3,] 3.5 3.9
## [4,] 3.6 4.0
##
## , , 6
##
## [,1] [,2]
## [1,] 4.1 4.5
## [2,] 4.2 4.6
## [3,] 4.3 4.7
## [4,] 4.4 4.8
#b.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)
B <-bar[c(4,1),2,]
B
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 0.8 1.6 2.4 3.2 4.0 4.8
## [2,] 0.5 1.3 2.1 2.9 3.7 4.5
#c.Use a fourfold repetition of the second row of the matrix formed in (b) to fill a new array of dimensions 2x2x2x3.
new <- array(rep(B[2,],times=4),dim=c(2,2,2,3))
new
## , , 1, 1
##
## [,1] [,2]
## [1,] 0.5 2.1
## [2,] 1.3 2.9
##
## , , 2, 1
##
## [,1] [,2]
## [1,] 3.7 0.5
## [2,] 4.5 1.3
##
## , , 1, 2
##
## [,1] [,2]
## [1,] 2.1 3.7
## [2,] 2.9 4.5
##
## , , 2, 2
##
## [,1] [,2]
## [1,] 0.5 2.1
## [2,] 1.3 2.9
##
## , , 1, 3
##
## [,1] [,2]
## [1,] 3.7 0.5
## [2,] 4.5 1.3
##
## , , 2, 3
##
## [,1] [,2]
## [1,] 2.1 3.7
## [2,] 2.9 4.5
#d.Create a new array comprised of the results of deleting the sixth layer of (a)
new_2 <- bar[,,-6]
new_2
## , , 1
##
## [,1] [,2]
## [1,] 0.1 0.5
## [2,] 0.2 0.6
## [3,] 0.3 0.7
## [4,] 0.4 0.8
##
## , , 2
##
## [,1] [,2]
## [1,] 0.9 1.3
## [2,] 1.0 1.4
## [3,] 1.1 1.5
## [4,] 1.2 1.6
##
## , , 3
##
## [,1] [,2]
## [1,] 1.7 2.1
## [2,] 1.8 2.2
## [3,] 1.9 2.3
## [4,] 2.0 2.4
##
## , , 4
##
## [,1] [,2]
## [1,] 2.5 2.9
## [2,] 2.6 3.0
## [3,] 2.7 3.1
## [4,] 2.8 3.2
##
## , , 5
##
## [,1] [,2]
## [1,] 3.3 3.7
## [2,] 3.4 3.8
## [3,] 3.5 3.9
## [4,] 3.6 4.0
#e.Overwrite the second and fourth row elements of the second column of layers 1, 3, and 5 of (d) with 99.
new_2[c(2,4),2,c(1,3,5)] <- -99
new_2
## , , 1
##
## [,1] [,2]
## [1,] 0.1 0.5
## [2,] 0.2 -99.0
## [3,] 0.3 0.7
## [4,] 0.4 -99.0
##
## , , 2
##
## [,1] [,2]
## [1,] 0.9 1.3
## [2,] 1.0 1.4
## [3,] 1.1 1.5
## [4,] 1.2 1.6
##
## , , 3
##
## [,1] [,2]
## [1,] 1.7 2.1
## [2,] 1.8 -99.0
## [3,] 1.9 2.3
## [4,] 2.0 -99.0
##
## , , 4
##
## [,1] [,2]
## [1,] 2.5 2.9
## [2,] 2.6 3.0
## [3,] 2.7 3.1
## [4,] 2.8 3.2
##
## , , 5
##
## [,1] [,2]
## [1,] 3.3 3.7
## [2,] 3.4 -99.0
## [3,] 3.5 3.9
## [4,] 3.6 -99.0
Chapter 4
Non-numeric Values
Exercise 4.1
#a. 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)
bop <- c(6,9,7,3,6,7,9,6,3,6,6,7,1,9,1)
#a.i.Those equal to 6
bop==6
## [1] TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE
## [13] FALSE FALSE FALSE
#a ii.Those greater than or equal to 6
bop>=6
## [1] TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE
## [13] FALSE TRUE FALSE
#a.iii. Those less than 6+2
bop<6+2
## [1] TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE
## [13] TRUE FALSE TRUE
#a.iv.Those not equal to 6
bop!=6
## [1] FALSE TRUE TRUE TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE TRUE
## [13] TRUE TRUE TRUE
#b.Create a new vector from the one used in (a) by deleting its first three elements. With this new vector, fill a 2x2x3 array
bop[-c(1:3)]
## [1] 3 6 7 9 6 3 6 6 7 1 9 1
mow <- array(bop[-c(1:3)],dim = c(2,2,3))
mow
## , , 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
#Examine the array for the following entries:
#b.i.Those less than or equal to 6 divided by 2, plus 4
B <- mow<=6/2+4
B
## , , 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
#b.ii.Those less than or equal to 6 divided by 2, plus 4, after increasing every element in the array by 2
C <- mow+2<=6/2+4
C
## , , 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
#c.Confirm the specific locations of elements equal to 0 in the 10x10 identity matrix I10
vee <- diag(x=10)
vee
## [,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
vee==0
## [,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
#d.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(B)
## [1] TRUE
all(B)
## [1] FALSE
any(C)
## [1] TRUE
all(C)
## [1] FALSE
#e.By extracting the diagonal elements of the logical matrix created in (c), use any to confirm there are no TRUE entries.
boa <- diag(vee)
boa
## [1] 1 1 1 1 1 1 1 1 1 1
any(boa)
## Warning in any(boa): coercing argument of type 'double' to logical
## [1] TRUE
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)
foo2 <- foo>5|foo==2
foo2
## [1] TRUE FALSE TRUE TRUE FALSE TRUE TRUE FALSE TRUE TRUE
#b.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)
bar2 <- bar<=6&bar!=4
bar2
## [1] FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE FALSE
#c. Identify the elements that satisfy (a) in foo AND satisfy (b) in bar
foo2&bar2
## [1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE TRUE FALSE
#d.Store a third vector called baz that is equal to the element-wise sum of foo and bar
baz <- c(foo+bar)
baz
## [1] 15 9 11 14 10 10 15 9 16 16
#d.i.The elements of baz greater than or equal to 14 but not equal to 15
baz>=14&baz!=15
## [1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE
#d.ii.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
bay <- baz/foo
bay
## [1] 2.142857 9.000000 1.571429 1.400000 2.000000 1.111111 1.500000 3.000000
## [9] 1.600000 2.000000
bay>4|bay<=2
## [1] FALSE TRUE TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE
#e. Confirm that using the long version in all of the preceding exercises performs only the first comparison (that is, the results each match the first entries of the previously obtained vectors)
Exercise 4.3
#a.Store this vector of 10 values: foo <- c(7,5,6,1,2,10,8,3,8,2).Then, do the following:
foo <- c(7,5,6,1,2,10,8,3,8,2)
#a.i.Extract the elements greater than or equal to 5, storing the result as bar
bar <- foo[foo>=5]
bar
## [1] 7 5 6 10 8 8
#a.ii.Display the vector containing those elements from foo that remain after omitting all elements that are greater than or equal to 5
vec <- foo[foo<5]
vec
## [1] 1 2 3 2
#b.Use bar from (a)(i) to construct a 2 3 matrix called baz, filled in a row-wise fashion.
baz <- matrix(data=bar,nrow=2,ncol=3,byrow=TRUE)
baz
## [,1] [,2] [,3]
## [1,] 7 5 6
## [2,] 10 8 8
#b.i Replace any elements that are equal to 8 with the squared value of the element in row 1, column 2 of baz itself
baz[baz==8] <- baz[1,2]^2
baz
## [,1] [,2] [,3]
## [1,] 7 5 6
## [2,] 10 25 25
#b.ii Confirm that all values in baz are now less than or equal to 25 AND greater than 4
all(baz<=255&baz>4)
## [1] TRUE
#c.
qux <- array(c(10, 5, 1, 4, 7, 4, 3, 3, 1, 3, 4, 3, 1, 7, 8, 3, 7, 3),dim=c(3,2,3))
qux
## , , 1
##
## [,1] [,2]
## [1,] 10 4
## [2,] 5 7
## [3,] 1 4
##
## , , 2
##
## [,1] [,2]
## [1,] 3 3
## [2,] 3 4
## [3,] 1 3
##
## , , 3
##
## [,1] [,2]
## [1,] 1 3
## [2,] 7 7
## [3,] 8 3
#c.i. Identify the dimension-specific index positions of elements that are either 3 OR 4.
which(qux==3|qux==4,arr.ind = T)
## dim1 dim2 dim3
## [1,] 1 2 1
## [2,] 3 2 1
## [3,] 1 1 2
## [4,] 2 1 2
## [5,] 1 2 2
## [6,] 2 2 2
## [7,] 3 2 2
## [8,] 1 2 3
## [9,] 3 2 3
#c. ii.Replace all elements in qux that are less than 3 OR greater than or equal to 7 with the value 100.
qux[qux<3|qux>=7] <- 100
qux
## , , 1
##
## [,1] [,2]
## [1,] 100 4
## [2,] 5 100
## [3,] 100 4
##
## , , 2
##
## [,1] [,2]
## [1,] 3 3
## [2,] 3 4
## [3,] 100 3
##
## , , 3
##
## [,1] [,2]
## [1,] 100 3
## [2,] 100 100
## [3,] 100 3
#d.Return to foo from (a). Use the vector c(F,T) to extract every second value from foo
foo[c(F,T)]
## [1] NA NA NA NA NA NA NA 7
foo[c(0,1)]
## [1] 7
Exercise 4.4
cat("\"The quick brown fox\n\tjumped over\n\t\tthe lazy dogs\"")
## "The quick brown fox
## jumped over
## the lazy dogs"
#b.Suppose you’ve stored the values num1 <- 4 and num2 <- 0.75. Write a line of R code that returns the following string: [1] "The result of multiplying 4 by 0.75 is 3"
num1 <- 4
num2 <- 0.75
paste("The result of multiplying",num1,"by",num2,"is",num1*num2)
## [1] "The result of multiplying 4 by 0.75 is 3"
#c.On my local machine, the directory for my work on this book is specified in R as "/Users/tdavies/Documents/RBook/". Imagine it is your machine—write a line of code that replaces tdavies in this string with your first initial and surname.
sub("tdavies","Asandra","/Users/tdavies/Documents/RBook/")
## [1] "/Users/Asandra/Documents/RBook/"
#d.
bar <- "How much wood could a woodchuck chuck"
#d.i Store a new string by gluing onto bar the words "if a woodchuck could chuck wood"
qux <- paste(bar,"if a woodchuck could chuck wood")
qux
## [1] "How much wood could a woodchuck chuck if a woodchuck could chuck wood"
#d.ii In the result of (i), replace all instances of wood with metal.
gsub(pattern="wood",replacement="metal",x=qux)
## [1] "How much metal could a metalchuck chuck if a metalchuck could chuck metal"
#e.Store the string "Two 6-packs for $12.99"
few <- "Two 6-packs for $12.99"
#e.i.Use a check for equality to confirm that the substring beginning with character 5 and ending with character 10 is "6-pack"
substr(x=few,start = 5,stop=10)=="6-pack"
## [1] TRUE
#e.ii. Make it a better deal by changing the price to $10.99
few2 <- sub("\\$12.99","$10.99",few)
few2
## [1] "Two 6-packs for $10.99"
Exercise 4.5
#The NewZealand government consists of the political parties National, Labour, Greens, and M¯ aori, with several smaller parties labeled as Other. Suppose you asked 20 New Zealanders which of these they identified most with and obtained the following data:
#There were12males and 8 females; the individuals numbered 1,5–7, 12, and 14–16 were females
#The individuals numbered 1, 4, 12, 15, 16, and 19 identified with Labour; no one identified with M¯aori; the individuals numbered 6, 9, and 11 identified with Greens; 10 and 20 identified with Other; and the rest identified with National
#a.Use your knowledge of vectors (for example, subsetting and overwriting) to create two character vectors: sex with entries "M" (male) and "F" (female) and party with entries "National","Labour", "Greens", "Maori", and "Other". Make sure the entries are placed in the correct positions as outlined earlier.
sex <- rep("M", times=20)
sex[c(1, 5:7, 12, 14:16)] <- "F"
sex
## [1] "F" "M" "M" "M" "F" "F" "F" "M" "M" "M" "M" "F" "M" "F" "F" "F" "M" "M" "M"
## [20] "M"
party <- rep("National", times=20)
party[c(1, 4, 12, 15, 16, 19)] <- "Labour"
party[c(6, 9, 11)] <- "Greens"
party[c(10, 20)] <- "Other"
party
## [1] "Labour" "National" "National" "Labour" "National" "Greens"
## [7] "National" "National" "Greens" "Other" "Greens" "Labour"
## [13] "National" "National" "Labour" "Labour" "National" "National"
## [19] "Labour" "Other"
#b.Create two different factor vectors based on sex and party. Does it make any sense to use ordered=TRUE in either case? How has R appeared to arrange the levels?
sex.fac <- factor(sex)
sex.fac
## [1] F M M M F F F M M M M F M F F F M M M M
## Levels: F M
party.fac <- factor(party)
party.fac
## [1] Labour National National Labour National Greens National National
## [9] Greens Other Greens Labour National National Labour Labour
## [17] National National Labour Other
## Levels: Greens Labour National Other
#Using ordered=TRUE
sex.factor <- factor(sex,ordered = T)
sex.factor
## [1] F M M M F F F M M M M F M F F F M M M M
## Levels: F < M
party.factor <- factor(party,ordered = T)
party.factor
## [1] Labour National National Labour National Greens National National
## [9] Greens Other Greens Labour National National Labour Labour
## [17] National National Labour Other
## Levels: Greens < Labour < National < Other
#c.Use factor subsetting to do the following: (i.)Return the factor vector of chosen parties for only the male participants.
party.factor[sex.factor=="M"]
## [1] National National Labour National Greens Other Greens National
## [9] National National Labour Other
## Levels: Greens < Labour < National < Other
#c.ii. Return the factor vector of genders for those who chose National.
sex.factor[party.factor=="National"]
## [1] M M F F M M F M M
## Levels: F < M
#d.Another six people joined the survey, with the results c("National","Maori","Maori","Labour","Greens","Labour") for the preferred party and c("M","M","F","F","F","M") as their gender.Combine these results with the original factors from (b)
new.party <- c("National","Maori","Maori","Labour","Greens","Labour")
new.party
## [1] "National" "Maori" "Maori" "Labour" "Greens" "Labour"
new.sex <- c("M","M","F","F","F","M")
new.sex
## [1] "M" "M" "F" "F" "F" "M"
latest.sex <- c(as.character(sex.factor),new.sex)
latest.sex
## [1] "F" "M" "M" "M" "F" "F" "F" "M" "M" "M" "M" "F" "M" "F" "F" "F" "M" "M" "M"
## [20] "M" "M" "M" "F" "F" "F" "M"
latest.party <- c(as.character(party.factor),new.party)
latest.party
## [1] "Labour" "National" "National" "Labour" "National" "Greens"
## [7] "National" "National" "Greens" "Other" "Greens" "Labour"
## [13] "National" "National" "Labour" "Labour" "National" "National"
## [19] "Labour" "Other" "National" "Maori" "Maori" "Labour"
## [25] "Greens" "Labour"
#Suppose you also asked all individuals to state how confident they were that Labour will win more seats in Parliament than National in the next election and to attach a subjective percentage to that confidence. The following 26 results were obtained: 93, 55, 29, 100,52, 84, 56, 0, 33, 52, 35, 53, 55, 46, 40, 40, 56, 45, 64, 31, 10, 29, 40,95, 18, 61
Y<- c(93, 55, 29, 100, 52, 84, 56, 0, 33, 52,
35, 53, 55, 46, 40, 40, 56, 45, 64, 31,
10, 29, 40, 95, 18, 61)
#e. Create a factor with levels of confidence as follows: Low for percentages [0,30]; Moderate for percentages (30,70]; and High for percentages (70,100]
br <- c(0, 30, 70, 100)
cut(x=Y,breaks = br)
## [1] (70,100] (30,70] (0,30] (70,100] (30,70] (70,100] (30,70] <NA>
## [9] (30,70] (30,70] (30,70] (30,70] (30,70] (30,70] (30,70] (30,70]
## [17] (30,70] (30,70] (30,70] (30,70] (0,30] (0,30] (30,70] (70,100]
## [25] (0,30] (30,70]
## Levels: (0,30] (30,70] (70,100]
cut(x=Y,breaks = br,right=T,include.lowest = T)
## [1] (70,100] (30,70] [0,30] (70,100] (30,70] (70,100] (30,70] [0,30]
## [9] (30,70] (30,70] (30,70] (30,70] (30,70] (30,70] (30,70] (30,70]
## [17] (30,70] (30,70] (30,70] (30,70] [0,30] [0,30] (30,70] (70,100]
## [25] [0,30] (30,70]
## Levels: [0,30] (30,70] (70,100]
lab <-c("Low", "Moderate", "High")
confidence <- cut(x=Y,breaks=br,labels =lab,right = T,include.lowest = T)
confidence
## [1] High Moderate Low High Moderate High Moderate Low
## [9] Moderate Moderate Moderate Moderate Moderate Moderate Moderate Moderate
## [17] Moderate Moderate Moderate Moderate Low Low Moderate High
## [25] Low Moderate
## Levels: Low Moderate High
#f.From (e), extract the levels corresponding to those individuals who originally said they identified with Labour. Do this also for National. What do you notice?
labour <- confidence[latest.party=="Labour"]
labour
## [1] High High Moderate Moderate Moderate Moderate High Moderate
## Levels: Low Moderate High
national <- confidence[latest.party=="National"]
national
## [1] Moderate Low Moderate Moderate Low Moderate Moderate Moderate
## [9] Moderate Low
## Levels: Low Moderate High
Chapter 5
Lists and Data Frames
Exercise 5.1
#a.Create a list that contains, in this order, a sequence of 20 evenly spaced numbers between 4 and 4; a 3 3 matrix of the logical vector c(F,T,T,T,F,T,T,F,F) filled column-wise;
#A character vector with the two strings "don" and "quixote"; and a factor vector containing the observations c("LOW","MED","LOW","MED","MED","HIGH"). Then, do the following:
foo <- list (seq(from=-4,to=4,length.out=20),matrix(data=c(FALSE,TRUE,TRUE,TRUE,FALSE,TRUE,TRUE,FALSE,FALSE),nrow=3,ncol=3), c("don","quixote"),factor(c("LOW","MED","LOW","MED","MED","HIGH")))
foo
## [[1]]
## [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
## [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263 0.2105263 0.6315789
## [13] 1.0526316 1.4736842 1.8947368 2.3157895 2.7368421 3.1578947
## [19] 3.5789474 4.0000000
##
## [[2]]
## [,1] [,2] [,3]
## [1,] FALSE TRUE TRUE
## [2,] TRUE FALSE FALSE
## [3,] TRUE TRUE FALSE
##
## [[3]]
## [1] "don" "quixote"
##
## [[4]]
## [1] LOW MED LOW MED MED HIGH
## Levels: HIGH LOW MED
#a.i.Extract row elements 2 and 1 of columns 2 and 3, in that order, of the logical matrix.
lag <- foo[[2]][c(2, 1), c(2, 3)]
lag
## [,1] [,2]
## [1,] FALSE FALSE
## [2,] TRUE TRUE
#a.ii. Use sub to overwrite "quixote" with "Quixote" and "don" with "Don" inside the list. Then, using the newly overwritten list member, concatenate to the console screen the following statement exactly:"Windmills! ATTACK!"-\Don Quixote/
foo[[3]] <- sub("don", "Don", foo[[3]])
foo[[3]] <- sub("quixote", "Quixote", foo[[3]])
foo
## [[1]]
## [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
## [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263 0.2105263 0.6315789
## [13] 1.0526316 1.4736842 1.8947368 2.3157895 2.7368421 3.1578947
## [19] 3.5789474 4.0000000
##
## [[2]]
## [,1] [,2] [,3]
## [1,] FALSE TRUE TRUE
## [2,] TRUE FALSE FALSE
## [3,] TRUE TRUE FALSE
##
## [[3]]
## [1] "Don" "Quixote"
##
## [[4]]
## [1] LOW MED LOW MED MED HIGH
## Levels: HIGH LOW MED
cat("\"Windmills! ATTACK!\"\n\t-\\Don Quixote/-\n")
## "Windmills! ATTACK!"
## -\Don Quixote/-
#a.iii. Obtain all values from the sequence between 4 and 4 that are greater than 1.
bar <- seq(from=-4,to=4,length.out=20)
bar
## [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
## [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263 0.2105263 0.6315789
## [13] 1.0526316 1.4736842 1.8947368 2.3157895 2.7368421 3.1578947
## [19] 3.5789474 4.0000000
bar>1
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [13] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#a.iv. Using which, determine which indexes in the factor vector are assigned the "MED" level.
bee <- factor(c("LOW","MED","LOW","MED","MED","HIGH"))
bee
## [1] LOW MED LOW MED MED HIGH
## Levels: HIGH LOW MED
which(x=bee=="MED")
## [1] 2 4 5
#b.Create a new list with the factor vector from (a) as a component named "facs";
#the numeric vector c(3,2.1,3.3,4,1.5,4.9) as a component named "nums";
#and a nested list comprised of the first three members of the list from (a) (use list slicing to obtain this),named "oldlist"
kid <- list(facs= c("LOW","MED","LOW","MED","MED","HIGH"),nums= c(3,2.1,3.3,4,1.5,4.9),oldlist=foo[1:3])
kid
## $facs
## [1] "LOW" "MED" "LOW" "MED" "MED" "HIGH"
##
## $nums
## [1] 3.0 2.1 3.3 4.0 1.5 4.9
##
## $oldlist
## $oldlist[[1]]
## [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
## [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263 0.2105263 0.6315789
## [13] 1.0526316 1.4736842 1.8947368 2.3157895 2.7368421 3.1578947
## [19] 3.5789474 4.0000000
##
## $oldlist[[2]]
## [,1] [,2] [,3]
## [1,] FALSE TRUE TRUE
## [2,] TRUE FALSE FALSE
## [3,] TRUE TRUE FALSE
##
## $oldlist[[3]]
## [1] "Don" "Quixote"
#b.i.Extract the elements of "facs" that correspond to elements of "nums" that are greater than or equal to 3
kid$facs[kid$nums>=3]
## [1] "LOW" "LOW" "MED" "HIGH"
#b.ii.Add a new member to the list named "flags". This member should be a logical vector of length 6, obtained as a twofold repetition of the third column of the logical matrix in the"oldlist" component
sad <- kid$oldlist[[2]][,3]
sad
## [1] TRUE FALSE FALSE
flags <- rep(sad,times=2)
flags
## [1] TRUE FALSE FALSE TRUE FALSE FALSE
kid$flags <- flags
kid
## $facs
## [1] "LOW" "MED" "LOW" "MED" "MED" "HIGH"
##
## $nums
## [1] 3.0 2.1 3.3 4.0 1.5 4.9
##
## $oldlist
## $oldlist[[1]]
## [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
## [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263 0.2105263 0.6315789
## [13] 1.0526316 1.4736842 1.8947368 2.3157895 2.7368421 3.1578947
## [19] 3.5789474 4.0000000
##
## $oldlist[[2]]
## [,1] [,2] [,3]
## [1,] FALSE TRUE TRUE
## [2,] TRUE FALSE FALSE
## [3,] TRUE TRUE FALSE
##
## $oldlist[[3]]
## [1] "Don" "Quixote"
##
##
## $flags
## [1] TRUE FALSE FALSE TRUE FALSE FALSE
#b.iii.Use "flags" and the logical negation operator ! to extract the entries of "num" corresponding to FALSE
kid$nums[!kid$flags]
## [1] 2.1 3.3 1.5 4.9
#b.iv. Overwrite the character string vector component of "oldlist" with the single character string "Don Quixote"
kid$oldlist[[3]] <- "Don Quixote"
kid
## $facs
## [1] "LOW" "MED" "LOW" "MED" "MED" "HIGH"
##
## $nums
## [1] 3.0 2.1 3.3 4.0 1.5 4.9
##
## $oldlist
## $oldlist[[1]]
## [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
## [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263 0.2105263 0.6315789
## [13] 1.0526316 1.4736842 1.8947368 2.3157895 2.7368421 3.1578947
## [19] 3.5789474 4.0000000
##
## $oldlist[[2]]
## [,1] [,2] [,3]
## [1,] FALSE TRUE TRUE
## [2,] TRUE FALSE FALSE
## [3,] TRUE TRUE FALSE
##
## $oldlist[[3]]
## [1] "Don Quixote"
##
##
## $flags
## [1] TRUE FALSE FALSE TRUE FALSE FALSE
Exercise 5.2
#a.
dframe <- data.frame(person=c("Stan","Francine","Steve","Roger","Hayley","Klaus"),sex=c("M","F","M","M","F","M"),funny=c("High","Med","Low","High","Med","Med"))
dframe
## person sex funny
## 1 Stan M High
## 2 Francine F Med
## 3 Steve M Low
## 4 Roger M High
## 5 Hayley F Med
## 6 Klaus M Med
#b.
age <- c("41","41","15","1600","21","60")
dframe <- cbind(dframe,age)
dframe
## person sex funny age
## 1 Stan M High 41
## 2 Francine F Med 41
## 3 Steve M Low 15
## 4 Roger M High 1600
## 5 Hayley F Med 21
## 6 Klaus M Med 60
#c.
dframe <- dframe[, c(1, 4, 2, 3)]
dframe
## person age sex funny
## 1 Stan 41 M High
## 2 Francine 41 F Med
## 3 Steve 15 M Low
## 4 Roger 1600 M High
## 5 Hayley 21 F Med
## 6 Klaus 60 M Med
#d.
mydata<-data.frame(person=c("Peter","Lois","Meg","Chris","Stewie"),
age=c(42,40,17,14,1),
sex=factor(c("M","F","F","M","M")),
stringsAsFactors=FALSE)
mydata
## person age sex
## 1 Peter 42 M
## 2 Lois 40 F
## 3 Meg 17 F
## 4 Chris 14 M
## 5 Stewie 1 M
newrecord <- data.frame(person="Brian",age=7,
sex=factor("M",levels=levels(mydata$sex)))
newrecord
## person age sex
## 1 Brian 7 M
mydata <- rbind(mydata,newrecord)
mydata
## person age sex
## 1 Peter 42 M
## 2 Lois 40 F
## 3 Meg 17 F
## 4 Chris 14 M
## 5 Stewie 1 M
## 6 Brian 7 M
funny<-c("High","High","Low","Med","High","Med")
funny<-factor(x=funny,levels=c("Low","Med","High"))
funny
## [1] High High Low Med High Med
## Levels: Low Med High
mydata<-cbind(mydata,funny)
mydata$age.mon<-mydata$age*12
mydata
## person age sex funny age.mon
## 1 Peter 42 M High 504
## 2 Lois 40 F High 480
## 3 Meg 17 F Low 204
## 4 Chris 14 M Med 168
## 5 Stewie 1 M High 12
## 6 Brian 7 M Med 84
mydata2 <- mydata[,-5]
mydata2
## person age sex funny
## 1 Peter 42 M High
## 2 Lois 40 F High
## 3 Meg 17 F Low
## 4 Chris 14 M Med
## 5 Stewie 1 M High
## 6 Brian 7 M Med
#e.
mydataframe <- rbind(dframe,mydata2)
mydataframe
## person age sex funny
## 1 Stan 41 M High
## 2 Francine 41 F Med
## 3 Steve 15 M Low
## 4 Roger 1600 M High
## 5 Hayley 21 F Med
## 6 Klaus 60 M Med
## 7 Peter 42 M High
## 8 Lois 40 F High
## 9 Meg 17 F Low
## 10 Chris 14 M Med
## 11 Stewie 1 M High
## 12 Brian 7 M Med
#f.
mydataframe[mydataframe$sex=="F"& mydataframe$funny==c("Med","High"),c("person","age")]
## person age
## 5 Hayley 21
## 8 Lois 40
#g
mydataframe[substr(x=mydataframe$person,start = 1,stop = 1)=="S",]
## person age sex funny
## 1 Stan 41 M High
## 3 Steve 15 M Low
## 11 Stewie 1 M High
Chapter 6
Special values ,Classes and Coercion
Exercise 6.1
#a. Store the following vector
foo <- c(13563,-14156,-14319,16981,12921,11979,9568,8833,-12968,8133)
foo
## [1] 13563 -14156 -14319 16981 12921 11979 9568 8833 -12968 8133
#a.i. Output all elements of foo that, when raised to a power of 75,are NOT infinite
foo[is.finite(foo^75)]
## [1] 11979 9568 8833 8133
#a.ii. Return the elements of foo, excluding those that result in negative infinity when raised to a power of 75
foo[foo^75 != -Inf]
## [1] 13563 16981 12921 11979 9568 8833 8133
#b. Store the following 3 4 matrix as the object bar:
bar <- matrix(data=c(7787540,2755145,2376430,-3647888,-3546625,-7333385,3659969,-7058569,-3980381,5597634,7669482,4703200),nrow=3,ncol=4,byrow=T)
bar
## [,1] [,2] [,3] [,4]
## [1,] 7787540 2755145 2376430 -3647888
## [2,] -3546625 -7333385 3659969 -7058569
## [3,] -3980381 5597634 7669482 4703200
#b i. Identify the coordinate-specific indexes of the entries of bar that are NaN when you raise bar to a power of 65 and divide by infinity.
which(is.nan(bar^65 / Inf), arr.ind = TRUE)
## row col
## [1,] 1 1
## [2,] 2 1
## [3,] 3 1
## [4,] 1 2
## [5,] 2 2
## [6,] 3 2
## [7,] 1 3
## [8,] 2 3
## [9,] 3 3
## [10,] 1 4
## [11,] 2 4
## [12,] 3 4
#b.ii. Return the values in bar that are NOT NaN when bar is raised to a power of 67 and infinity is added to the result. Confirm this is identical to identifying those values in bar that, when raised to a power of 67, are not equal to negative infinity
not_NaN <- bar[!is.nan(bar^67 + Inf)]
not_NaN
## [1] 7787540 2755145 5597634 2376430 3659969 7669482 4703200
not_negInf <- bar[bar^67 != -Inf]
not_negInf
## [1] 7787540 2755145 5597634 2376430 3659969 7669482 4703200
identical(not_NaN, not_negInf)
## [1] TRUE
#b.iii. Identify those values in bar that are either negative infinity OR finite when you raise bar to a power of 67.
bar[(bar^67 == -Inf) | is.finite(bar^67)]
## [1] -3546625 -3980381 -7333385 -3647888 -7058569
##Exercise 6.2##
#a. Consider the following line of code
foo <- c(4.3,2.2,NULL,2.4,NaN,3.3,3.1,NULL,3.4,NA)
foo
## [1] 4.3 2.2 2.4 NaN 3.3 3.1 3.4 NA
#a.i. The length of foo is 8
length(foo==8)
## [1] 8
#a.ii. Calling which(x=is.na(x=foo)) will not result in 4 and 8
which(x=is.na(x=foo))
## [1] 4 8
#a.iii. Checking is.null(x=foo) will provide you with the locations of the two NULL values that are present
is.null(x=foo)
## [1] FALSE
#a.iv.Executing is.na(x=foo[8])+4/NULL will not result in NA.
is.na(x=foo[8])+4/NULL
## numeric(0)
#b. Create and store a list containing a single member: the vector c(7,7,NA,3,NA,1,1,5,NA). Then, do the following:
bar <- c(7, 7, NA, 3, NA, 1, 1, 5, NA)
bar
## [1] 7 7 NA 3 NA 1 1 5 NA
#b.i. Name the member "alpha"
barlist <- list("alpha"=bar)
barlist
## $alpha
## [1] 7 7 NA 3 NA 1 1 5 NA
#b.ii. Confirm that the list doesn’t have a member with the name "beta" using the appropriate logical valued function.
barlist$beta
## NULL
#b.iii. Add a new member called beta, which is the vector obtained by identifying the index positions of alpha that are NA
Na <- which(is.na(bar))
Na
## [1] 3 5 9
barlist$beta <- Na
barlist
## $alpha
## [1] 7 7 NA 3 NA 1 1 5 NA
##
## $beta
## [1] 3 5 9
Exercise 6.3
#a.Identify the class of the following objects. For each object, also state whether the class is explicitly or implicitly defined
foo <- array(data=1:36,dim=c(3,3,4))
foo
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 10 13 16
## [2,] 11 14 17
## [3,] 12 15 18
##
## , , 3
##
## [,1] [,2] [,3]
## [1,] 19 22 25
## [2,] 20 23 26
## [3,] 21 24 27
##
## , , 4
##
## [,1] [,2] [,3]
## [1,] 28 31 34
## [2,] 29 32 35
## [3,] 30 33 36
class(foo)
## [1] "array"
# foo is explicitly defined
bar <- as.vector(foo)
bar
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30 31 32 33 34 35 36
class(bar)
## [1] "integer"
# bar is implicitly defined
baz <- as.character(bar)
baz
## [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15"
## [16] "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30"
## [31] "31" "32" "33" "34" "35" "36"
class(baz)
## [1] "character"
# baz is explicitly defined
qux <- as.factor(baz)
qux
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [26] 26 27 28 29 30 31 32 33 34 35 36
## 36 Levels: 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 26 27 28 ... 9
class(qux)
## [1] "factor"
# qux is explicitly defined
quux <- bar+c(-0.1,0.1)
quux
## [1] 0.9 2.1 2.9 4.1 4.9 6.1 6.9 8.1 8.9 10.1 10.9 12.1 12.9 14.1 14.9
## [16] 16.1 16.9 18.1 18.9 20.1 20.9 22.1 22.9 24.1 24.9 26.1 26.9 28.1 28.9 30.1
## [31] 30.9 32.1 32.9 34.1 34.9 36.1
class(quux)
## [1] "numeric"
# quux is implicitly defined
#b For each object defined in (a), find the sum of the result of calling is.numeric and is.integer on it separately.
is.numeric(foo)+is.integer(foo)
## [1] 2
is.numeric(bar)+is.integer(bar)
## [1] 2
is.numeric(baz)+is.integer(baz)
## [1] 0
is.numeric(qux)+is.integer(qux)
## [1] 0
is.numeric(quux)+is.integer(quux)
## [1] 1
#Turn the collection of five results into a factor with levels 0, 1, and 2, identified by the results themselves. Compare this factor vector with the result of coercing it to a numeric vector
test <- c(is.numeric(foo)+is.integer(foo),
is.numeric(bar)+is.integer(bar),
is.numeric(baz)+is.integer(baz),
is.numeric(qux)+is.integer(qux),
is.numeric(quux)+is.integer(quux))
test
## [1] 2 2 0 0 1
fac <- factor(test,levels=c(0,1,2))
fac
## [1] 2 2 0 0 1
## Levels: 0 1 2
as.numeric(fac)
## [1] 3 3 1 1 2
#c.
M <- matrix(c( 2,3,4,5,6,7,8,9,10,11,12,13),nrow=3,ncol=4)
M
## [,1] [,2] [,3] [,4]
## [1,] 2 5 8 11
## [2,] 3 6 9 12
## [3,] 4 7 10 13
as.character(M)
## [1] "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13"
# Store the following matrix: Then, do the following:
mat <- matrix(c(34,23,33,42,41,0,1,1,0,0,1,2,1,1,2),nrow=5,ncol=3)
mat
## [,1] [,2] [,3]
## [1,] 34 0 1
## [2,] 23 1 2
## [3,] 33 1 1
## [4,] 42 0 1
## [5,] 41 0 2
#d.i Coerce the matrix to a data frame.
sed <- as.data.frame(mat)
sed
## V1 V2 V3
## 1 34 0 1
## 2 23 1 2
## 3 33 1 1
## 4 42 0 1
## 5 41 0 2
#d.ii. As a data frame, coerce the second column to be logical valued
as.logical(sed[[2]])
## [1] FALSE TRUE TRUE FALSE FALSE
#d.iii. As a data frame, coerce the third column to be factor-valued.
as.factor(sed[[3]])
## [1] 1 2 1 1 2
## Levels: 1 2