module 11

Author

Rachael Berghahn

library(data.table)
Warning: package 'data.table' was built under R version 4.3.3
library(dplyr)
Warning: package 'dplyr' was built under R version 4.3.3

Attaching package: 'dplyr'
The following objects are masked from 'package:data.table':

    between, first, last
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
temp <- fread("Temperature.csv") ##reading in data using fread
temp$Season <- factor(temp$Season)
winter <- filter(temp, Season == "winter") ##extracting winter observations
winterNC <- filter(temp, Season == "winter", Area == "NC") ##winter observations for NC
area_season_temp <- select(temp, Area, Season, Temperature) ##selecting columns for area, season, temperature
winter_area_temp <- filter(temp, Season == "winter") %>%
  select(Area, Temperature) ##selecting for area and temperature in winter
number_winter <- nrow(winter) ##number of observations in winter
print(number_winter)
[1] 1706
winter_means <- winter %>%
  summarise(
    mean_temp = mean(Temperature, na.rm = TRUE),
    mean_sal = mean(Salinity, na.rm = TRUE)
  )
winter_means  ##calculating means of temperature and salinity in winter
  mean_temp mean_sal
1   5.57162 29.15756
station_winter <- winter %>%
  count(Station) 
  station_winter  ##number of observations per station in winter
    Station     n
     <char> <int>
 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
station_season <- temp %>%
  count(Station, Season) 
  station_season ##number of observations per station per season
     Station Season     n
      <char> <fctr> <int>
  1:    DANT autumn    72
  2:    DANT spring    89
  3:    DANT summer    89
  4:    DANT winter    50
  5:    DREI autumn    61
 ---                     
114:    ZIJP winter    54
115:    ZUID autumn    73
116:    ZUID spring    89
117:    ZUID summer    89
118:    ZUID winter    52
temp_month <- temp %>%
  group_by(Month) %>%
  summarise(
    mean_temp = mean(Temperature, na.rm = TRUE),
    count = n()
  )
  temp_month  ##avg temp by month
# A tibble: 12 × 3
   Month mean_temp count
   <int>     <dbl> <int>
 1     1      5.17   604
 2     2      4.74   542
 3     3      6.13   768
 4     4      8.70   730
 5     5     12.3    898
 6     6     15.7    829
 7     7     18.1    821
 8     8     19.4    894
 9     9     17.0    699
10    10     13.6    617
11    11      9.85   566
12    12      6.75   560
temp_month_area <- temp %>%
  group_by(Month, Area) %>%
  summarise(mean_temp = mean(Temperature, na.rm = TRUE), count = n(), .groups = 'drop') 
  temp_month_area   ##avg temp by month by area
# A tibble: 120 × 4
   Month Area  mean_temp count
   <int> <chr>     <dbl> <int>
 1     1 ED         3.09    32
 2     1 GM         4.31    17
 3     1 KZ         5.30   174
 4     1 NC         6.79    65
 5     1 NZ         8.12    37
 6     1 OS         4.87    76
 7     1 VD         5.16    90
 8     1 VM         4.31    17
 9     1 WS         4.98    46
10     1 WZ         3.38    50
# ℹ 110 more rows
library(ggplot2)
ggplot(temp_month_area, aes(x = Month, y = mean_temp, color = Area)) +
  geom_line() +
  labs(title = "Average Temperature by Month and Area",
       x = "Month",
       y = "Average Temperature")


Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

1 + 1
[1] 2

You can add options to executable code like this

[1] 4

The echo: false option disables the printing of code (only output is displayed).