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")

Extract all winter observations

winterTemp = temp[Season == "winter"]
head(winterTemp)
##           Sample     Date     DateNr dDay1 dDay2 dDay3 Station Area 31UE_ED50
## 1: DANT.19900110 19900110    10/1/90     7     9     9    DANT   WZ  681379.6
## 2: DANT.19900206 19900206     6/2/90    34    36    36    DANT   WZ  681379.6
## 3: DANT.19901212 19901212   12/12/90   343   345   345    DANT   WZ  681379.6
## 4: DANT.19910116 19910116  1/16/1991   378   380    15    DANT   WZ  681379.6
## 5: DANT.19910226 19910226  2/26/1991   419   421    56    DANT   WZ  681379.6
## 6: DANT.19911219 19911219 12/19/1991   715   717   352    DANT   WZ  681379.6
##    31UN_ED50 Year Month Season Salinity Temperature CHLFa
## 1:   5920571 1990     1 winter    29.19         4.0  1.30
## 2:   5920571 1990     2 winter    27.37         6.0    NA
## 3:   5920571 1990    12 winter    31.50         4.2 60.50
## 4:   5920571 1991     1 winter    20.83        -0.3  2.30
## 5:   5920571 1991     2 winter    28.06         3.9  3.52
## 6:   5920571 1991    12 winter    25.31         3.9  3.50

Extract all winter observations for zone NC

winterNC = temp[Season == "winter" & Area == "NC"]
head(winterNC)
##           Sample     Date     DateNr dDay1 dDay2 dDay3 Station Area 31UE_ED50
## 1: T100.19900103 19900103     3/1/90     0     2     2    T100   NC  587650.2
## 2: T100.19900205 19900205     5/2/90    33    35    35    T100   NC  587650.2
## 3: T100.19901218 19901218 12/18/1990   349   351   351    T100   NC  587650.2
## 4: T100.19910116 19910116  1/16/1991   378   380    15    T100   NC  587650.2
## 5: T100.19910205 19910205     5/2/91   398   400    35    T100   NC  587650.2
## 6: T100.19911211 19911211   11/12/91   707   709   344    T100   NC  587650.2
##    31UN_ED50 Year Month Season Salinity Temperature CHLFa
## 1:   6001110 1990     1 winter    34.82         8.5  0.30
## 2:   6001110 1990     2 winter       NA          NA    NA
## 3:   6001110 1990    12 winter    34.80         9.2  0.40
## 4:   6001110 1991     1 winter    34.86         6.1  0.68
## 5:   6001110 1991     2 winter    34.53         5.2  0.34
## 6:   6001110 1991    12 winter    34.79         9.7  0.44

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()