library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ ggplot2 3.3.3 ✔ purrr 0.3.4
## ✔ tibble 3.1.1 ✔ dplyr 1.0.5
## ✔ tidyr 1.1.3 ✔ stringr 1.4.0
## ✔ readr 1.4.0 ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
Load the data…
temperature <- read_table(
"https://chryswoods.com/data_analysis_r/cetml1659on.txt",
skip=6,
na=c("-99.99", "-99.9"),
col_types=cols("DATE"=col_integer())
)
Create the month levels
month_levels <- c("JAN", "FEB", "MAR", "APR", "MAY", "JUN",
"JUL", "AUG", "SEP", "OCT", "NOV", "DEC")
Tidy the data…
historical_temperature <- temperature %>%
select(-YEAR) %>%
pivot_longer(c("JAN", "FEB", "MAR", "APR", "MAY", "JUN",
"JUL", "AUG", "SEP", "OCT", "NOV", "DEC"),
names_to="month",
values_to="temperature") %>%
rename(year=DATE) %>%
mutate(month=factor(month, month_levels))
Calculate the mean monthly temperatures in the 17th Century
c17th <- historical_temperature %>%
filter(year<1700 & year>=1600) %>%
group_by(month) %>%
summarise("temperature"=mean(temperature, na.rm=TRUE), .groups="drop")
(the .groups="drop"
removes a warning message in newer versions of R. It is experimental, e.g. see this stackoverflow post)
Calculate the mean monthly temperatures in the 21st Century
c21st <- historical_temperature %>%
filter(year>=2000) %>%
group_by(month) %>%
summarise("temperature"=mean(temperature, na.rm=TRUE), .groups="drop")
Now add the difference to the c21st table and print it out
c21st["change"] <- c21st["temperature"] - c17th["temperature"]
c21st
From this we can see that most of the warming is focused on the winter months.
We will now repeat this for the maximum and minimum temperatures…
c17th_max <- historical_temperature %>%
filter(year<1700 & year>=1600) %>%
group_by(month) %>%
summarise("temperature"=max(temperature, na.rm=TRUE), .groups="drop")
c21st_max <- historical_temperature %>%
filter(year>=2000) %>%
group_by(month) %>%
summarise("temperature"=max(temperature, na.rm=TRUE), .groups="drop")
c21st_max["change"] <- c21st_max["temperature"] - c17th_max["temperature"]
c21st_max
c17th_min <- historical_temperature %>%
filter(year<1700 & year>=1600) %>%
group_by(month) %>%
summarise("temperature"=min(temperature, na.rm=TRUE), .groups="drop")
c21st_min <- historical_temperature %>%
filter(year>=2000) %>%
group_by(month) %>%
summarise("temperature"=min(temperature, na.rm=TRUE), .groups="drop")
c21st_min["change"] <- c21st_min["temperature"] - c17th_min["temperature"]
c21st_min
Finally, we can get the average increase in monthly temperatures by calculating the mean of the change
column in c21st
mean(c21st[["change"]])
## [1] 1.643698