These are my solutions to the exercises on R Worksheet 3. This R code is not the only way to solve these exercises – although your numerical answers should agree (up to rounding).
Exercise 3.1. Copy the command above to read in the Met Office temperature data to an object called
temperature. How many rows does the data have?
I started by reading the temperature data into R.
temperature <- read.csv("https://mpaldridge.github.io/math1710/data/met-office.csv")
I then found the number of rows in the data
nrow(temperature)
[1] 136
Exercise 3.2 We continue with the
temperaturedata.
(a) What year does the 40th row correspond to?
temperature[40, "year"]
[1] 1923
(b) What was the temperature in August of that year?
temperature[40, "aug"]
[1] 13.7
(c) Output the whole list of January temperatures.
temperature$jan
[1] 4.9 1.8 0.8 1.3 2.5 2.7 4.5 0.8 1.1 1.5 2.0 -1.1 3.8 0.4 5.6 3.4 3.1 2.5 3.6 2.9 3.1 2.9 4.2
[24] 2.6 1.8 2.3 2.1 3.2 2.3 2.6 2.8 2.9 6.4 1.0 2.6 1.9 3.6 5.7 2.1 4.8 3.2 4.1 3.4 3.5 4.0 0.9
[47] 4.1 2.2 5.1 1.6 3.7 3.6 2.3 3.9 4.6 2.8 -2.0 -0.6 0.2 3.2 4.8 -0.4 2.0 1.4 3.7 4.2 3.5 2.4 1.4
[70] 3.1 2.1 1.6 2.3 4.5 2.0 0.4 2.9 2.3 3.3 -1.5 2.8 2.3 1.9 3.3 3.3 4.1 2.4 3.5 3.0 3.7 4.7 5.5
[93] 4.6 1.4 1.9 -1.0 1.4 3.5 1.7 5.4 2.1 0.4 2.2 0.4 4.0 5.6 5.4 2.1 3.0 4.5 3.7 3.1 3.4 2.0 4.1
[116] 4.5 4.2 2.4 4.5 3.8 4.3 5.2 3.6 5.9 5.4 2.7 0.7 2.9 4.3 2.8 4.6 3.5 4.5 3.5 4.0 3.4
(d) Output the December temperature for rows 50 to 60.
temperature[50:60, "dec"]
[1] 1.7 6.6 1.8 4.2 1.9 3.4 2.8 3.3 4.7 5.3 3.1
Exercise 3.3. We continue with the
temperaturedata.
(a) What was the median temperature in September?
median(temperature$sep)
[1] 12.4
(b) What is the sample variance of the first fifty years of February data?
answer33b <- var(temperature[1:50, "feb"])
round(answer33b, digits = 2)
[1] 2.62
(c) What is the lowest December temperature?
min(temperature$dec)
[1] -1.2
(d) What is the correlation between October and November temperatures, restricted only to the first 100 years of data?
answer33d <- cor(temperature[1:100, "oct"], temperature[1:100, "nov"])
signif(answer33d, digits = 2)
[1] 0.083