Load in data.table library and data
library(data.table)
## Warning: package 'data.table' was built under R version 4.0.5
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.0.5
library(magrittr)
## Warning: package 'magrittr' was built under R version 4.0.5
temp = fread("Temperature.csv")
Select only the columns Area, Season and Temperature
areaSeasTemp = temp[, .(Area, Season, Temperature)]
areaSeasTemp
## Area Season Temperature
## 1: WZ winter 4.00
## 2: WZ winter 6.00
## 3: WZ spring 7.30
## 4: WZ spring 8.20
## 5: WZ spring 17.40
## ---
## 8524: WZ autumn 15.47
## 8525: WZ autumn 13.45
## 8526: WZ autumn 12.09
## 8527: WZ autumn 9.03
## 8528: WZ winter 5.13
Select only the columns Area and Temperature but only for winter observations
winterAreaTemp = temp[Season == "winter", .(Area, Temperature)]
winterAreaTemp
## Area Temperature
## 1: WZ 4.00
## 2: WZ 6.00
## 3: WZ 4.20
## 4: WZ -0.30
## 5: WZ 3.90
## ---
## 1702: WZ 4.40
## 1703: WZ 4.72
## 1704: WZ 3.57
## 1705: WZ 1.31
## 1706: WZ 5.13
Find the total number of observations in winter
winterObs = temp[Season == "winter", .N]
winterObs
## [1] 1706
print("The total number of winter observations is 1706")
## [1] "The total number of winter observations is 1706"
Calculate the mean temperature and mean salinity in winter (Note that there are missing values so will have to use na.rm = TRUE)
winterMean = temp[Season == "winter",
.(m_temp = mean(Temperature, na.rm = TRUE), m_salinity = mean(Salinity, na.rm = TRUE))]
winterMean
## m_temp m_salinity
## 1: 5.57162 29.15756
Find the number of observations per station in winter
stationWinter = temp[Season == "winter", .N, by = Station]
stationWinter
## Station N
## 1: DANT 50
## 2: DREI 52
## 3: G6 101
## 4: GROO 50
## 5: HAMM 55
## 6: HANS 56
## 7: HUIB 50
## 8: LODS 54
## 9: MARS 49
## 10: N02 115
## 11: N10 131
## 12: N20 50
## 13: N70 50
## 14: R03 32
## 15: SOEL 50
## 16: T004 97
## 17: T010 45
## 18: T100 45
## 19: T135 46
## 20: T175 45
## 21: T235 45
## 22: VLIS 84
## 23: W02 99
## 24: W20 47
## 25: W70 47
## 26: WISS 55
## 27: ZIJP 54
## 28: ZUID 52
## Station N
Find the number of observations per station per season
stationSeason = temp[, .N, by = .(Station, Season)]
stationSeason
## Station Season N
## 1: DANT winter 50
## 2: DANT spring 89
## 3: DANT summer 89
## 4: DANT autumn 72
## 5: DREI winter 52
## ---
## 114: ZIJP autumn 61
## 115: ZUID winter 52
## 116: ZUID spring 89
## 117: ZUID summer 89
## 118: ZUID autumn 73
Estimate average temperatures by month
avgTempMonth = temp[,.(m_temp = mean(Temperature, na.rm = TRUE)),by = .(Month)]
avgTempMonth
## Month m_temp
## 1: 1 5.174210
## 2: 2 4.737400
## 3: 3 6.125961
## 4: 4 8.702035
## 5: 5 12.293479
## 6: 6 15.659933
## 7: 7 18.077343
## 8: 8 19.388355
## 9: 9 16.995974
## 10: 10 13.619670
## 11: 11 9.848891
## 12: 12 6.746339
Estimate average temperatures by month by area
avgTempMonthArea = temp[,.(m_temp = mean(Temperature, na.rm = TRUE)),by = .(Month, Area)]
avgTempMonthArea
## Month Area m_temp
## 1: 1 WZ 3.377826
## 2: 2 WZ 3.925800
## 3: 3 WZ 5.818481
## 4: 4 WZ 9.270805
## 5: 5 WZ 13.398191
## ---
## 116: 1 NC 6.789808
## 117: 2 NC 5.682581
## 118: 3 NC 5.837500
## 119: 11 NC 10.978269
## 120: 12 NC 8.716957
Plot the output of the previous question using ggplot2 using the geom_line() geometry
avgTempMonthArea = temp[,.(m_temp = mean(Temperature, na.rm = TRUE)),by = .(Month, Area)]
ggplot(avgTempMonthArea, aes(x = Month, y = m_temp, col = Area)) + geom_line()
