This exercise will use the dataset Temperature.csv, a file with sea temperature and salinity recorded from 31 stations over 15 years.The stations are located in one of 10 areas.

Step one: Read in the data

library(data.table)
temp <- fread("Temperature.csv")
dim(temp)
## [1] 8528   16
class(temp)
## [1] "data.table" "data.frame"

Step two: Extract all winter observations

temp_winter <- temp[Season == "winter"]
head(temp_winter)
##           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

Step three: Extract all winter observations for zone NC

temp_winter_NC <- temp[Season == "winter" & Area == "NC"]
head(temp_winter_NC)
##           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

Step four: Select only the columns Area, Season and Temperature

temp_selected_area_season_temp <- temp[, .(Area, Season, Temperature)]
head(temp_selected_area_season_temp)
##    Area Season Temperature
## 1:   WZ winter         4.0
## 2:   WZ winter         6.0
## 3:   WZ spring         7.3
## 4:   WZ spring         8.2
## 5:   WZ spring        17.4
## 6:   WZ summer        18.1

Step five: Select only the columns Area and Temperature but only for winter observations

temp_selected_winter <- temp[Season == "winter", .(Area, Temperature)]
head(temp_selected_winter)
##    Area Temperature
## 1:   WZ         4.0
## 2:   WZ         6.0
## 3:   WZ         4.2
## 4:   WZ        -0.3
## 5:   WZ         3.9
## 6:   WZ         3.9

Step six: Find the total number of observations in winter

winter_observations <- temp[Season == "winter", .N]
winter_observations
## [1] 1706

Step seven: Calculate the mean temperature and mean salinity in winter (Note that there are missing values so will have to use na.rm = TRUE)

winter_mean_temp <- temp[Season == "winter", .(mean_temp = mean(Temperature, na.rm = TRUE))]
winter_mean_temp
##    mean_temp
## 1:   5.57162
winter_mean_salinity <- temp[Season == "winter", .(mean_salinity = mean(Salinity, na.rm = TRUE))]
winter_mean_salinity
##    mean_salinity
## 1:      29.15756

Step eight: Find the number of observations per station in winter

winter_observations_per_station <- temp[Season == "winter", .N, by = Station]
winter_observations_per_station
##     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

Step nine: Find the number of observations per station per season

observations_per_station_per_season <- temp[, .N, by = .(Station, Season)]
observations_per_station_per_season
##      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

Step ten: Estimate average temperatures by month

mean_temp_by_month <- temp[, .(mean_temp = mean(Temperature, na.rm = TRUE)), by = Month]
mean_temp_by_month
##     Month mean_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

Step eleven: Estimate average temperatures by month by area

mean_temp_by_month_by_area <- temp[, .(mean_temp = mean(Temperature, na.rm = TRUE)), by = .(Month, Area)]
mean_temp_by_month_by_area
##      Month Area mean_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

Step twelve: Plot the output of the previous question using ggplot2 using the geom_line() geometry

library(ggplot2)

ggplot(mean_temp_by_month_by_area, aes(x = Month, y = mean_temp, color = Area, group = Area)) +
  geom_line() +
  labs(x = "Month", y = "Mean Temperature", color = "Area") +
  theme_minimal()