Chapter 16 Solutions to Selected Practice Problems

2.5.3

a <- (exp(14) + log10(8)) * sqrt(5)
b <- log(4) - (5 * 10^2)
a / b

4.1.3

tree.sp[length(tree.sp) - 1]

4.3.1

my.weights[my.weights == 12] <- NA
sd(my.weights, na.rm = TRUE)

4.8.1

sim.vals <- rnorm(10000)
hist(sim.vals)

5.3.2

ggplot(data = alc.dat, aes(x = alc.drinks, y = alc.weights)) +
geom_point(col = 'blue', size = 2, shape = "square") +
labs(x = "Number of Drinks",
y = "Weight")

6.4.3 (a)

apply(WeatherKLAN2014Full[,c(2:19, 21, 23)], 2, sd, na.rm = TRUE)

6.4.3 (b)

apply(iris[, -5], 2, median)

6.7.2

separate(data = data.birds, col = recordingInfo, sep = "-",
into = c("site", "year", "month", "day"))

6.8.4

select(gm, contains('c'))

6.8.7

gm %>%
filter(country == 'Afghanistan') %>%
select(c("year", "lifeExp")) %>%
arrange(desc(lifeExp))

6.8.11

iris %>%
mutate(s.p.ratio = Sepal.Length / Petal.Length) %>%
group_by(Species) %>%
summarize(mean.ratio = mean(s.p.ratio)) %>%
arrange(desc(mean.ratio))

7.1.1

FtoK <- function(F) {
K <- ((F - 32) * (5 / 9)) + 273.15
return(K)
}

7.4.4

a[a > 1] <- 0

10.2.2

falsePositives <- rep(0, 40)
for (i in 1:length(falsePositives)) {
knn_Pima <- knn(Pima.tr[,c(2,5)], Pima.te[,c(2,5)], Pima.tr[,8], k = i, prob=TRUE)
falsePositives[i] <- table(knn_Pima, Pima.te[,8])[2, 1]
}
# Using graphics
plot(falsePositives, pch = 19, las = 1, xlab = "k", ylab = "# of False Positives")
# Using ggplot2
dat <- data.frame(k = 1:40, falsePositives)
ggplot(data = dat, aes(x = k, y = falsePositives)) +
geom_point() +
labs(y = "# of False Positives")

11.3.3

unlist(strsplit(strings, split = '[-|.]'))

11.3.5

library(wordcloud)
md.data.frame <- as.data.frame(moby_dick_word_table)
wordcloud(md.data.frame$moby_dick, md.data.frame$Freq, max.words = 500, colors = rainbow(20), random.color = TRUE)