Homework Module 1.4 Key

Your Name

Date

In the questions below, use R code to answer questions. For any non-coding questions, give your answer as a comment.

Run this code chunk first, to load the tidyverse, and make the weather station data available as the variable stationData:

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
stationData<-read_csv("station-data.csv")
Rows: 139160 Columns: 12
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr  (1): station_id
dbl (11): year, day, month, running_day, hour, temp, pressure, wind_speed, w...

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

What is the difference between the data in the day and running_day columns? Illustrate the difference with a function we learned in class. If we’re grouping by month, which would be more useful?

# day refers to day in a year (e.g. 1-365), and running_day is day of the month

unique(stationData$day)
  [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
 [19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
 [37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
 [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
 [73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
 [91]  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108
[109] 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
[127] 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
[145] 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
[163] 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
[181] 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
[199] 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
[217] 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
[235] 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
[253] 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
[271] 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
[289] 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
[307] 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
[325] 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
[343] 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
[361] 361 362 363 364 365
unique(stationData$running_day)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31

How is the time of day represented in the data. At what times of the day are measurements taken? Show the code used to arrive at the answer.

# time is represented in military time.
# measurements are taken every three hours, starting at midnight

unique(stationData$hour)
[1]    0  300  600  900 1200 1500 1800 2100

Report the mean and standard deviation of the atmospheric pressure for every hour, of every day, of every month.

stationData %>%
  group_by(month, running_day, hour) %>%
  summarize(meanPressure=mean(pressure, na.rm=TRUE), sdPressure=sd(pressure, na.rm=TRUE))
`summarise()` has grouped output by 'month', 'running_day'. You can override
using the `.groups` argument.
# A tibble: 2,920 × 5
# Groups:   month, running_day [365]
   month running_day  hour meanPressure sdPressure
   <dbl>       <dbl> <dbl>        <dbl>      <dbl>
 1     1           1     0         886.       123.
 2     1           1   300         884.       123.
 3     1           1   600         886.       123.
 4     1           1   900         884.       123.
 5     1           1  1200         884.       123.
 6     1           1  1500         884.       123.
 7     1           1  1800         885.       123.
 8     1           1  2100         888.       123.
 9     1           2     0         886.       123.
10     1           2   300         886.       124.
# ℹ 2,910 more rows

Report the mean and standard deviation of the temperature and wind speed at noon of every day of every month.

stationData %>%
  filter(hour=="1200") %>%
  group_by(month, running_day) %>%
  summarize(avgTemp=mean(temp, na.rm=TRUE),
            sdTemp=sd(temp, na.rm=TRUE),
            avgWindSpeed=mean(wind_speed, na.rm=TRUE),
            sdWindSpeed=sd(wind_speed, na.rm=TRUE)
            )
`summarise()` has grouped output by 'month'. You can override using the
`.groups` argument.
# A tibble: 365 × 6
# Groups:   month [12]
   month running_day avgTemp sdTemp avgWindSpeed sdWindSpeed
   <dbl>       <dbl>   <dbl>  <dbl>        <dbl>       <dbl>
 1     1           1  -10.2    6.90         4.45        3.19
 2     1           2  -10.3    7.24         4.36        3.10
 3     1           3  -10.1    7.49         4.9         4.15
 4     1           4   -9.89   7.07         4.9         3.25
 5     1           5   -9.73   6.62         4.14        3.02
 6     1           6   -9.62   6.67         4.83        2.62
 7     1           7   -9.29   6.39         4.92        3.66
 8     1           8   -8.27   6.65         4.58        3.57
 9     1           9   -8.92   6.61         3.90        3.62
10     1          10  -10.8    6.81         4.08        2.72
# ℹ 355 more rows

Find the coldest and warmest days of the year (in month, day format).

#coldest
stationData %>%
  select(month, running_day, temp) %>%
  arrange(temp) %>%
  head(1)
# A tibble: 1 × 3
  month running_day  temp
  <dbl>       <dbl> <dbl>
1     8          29 -80.2
# August 29th

#warmest
stationData %>%
  select(month, running_day, temp) %>%
  arrange(desc(temp)) %>%
  head(1)
# A tibble: 1 × 3
  month running_day  temp
  <dbl>       <dbl> <dbl>
1    12           9   9.6
# December 9th

Add a column to the data frame that contains the temperature in Kelvins.

# add 273 to celcius

stationData <- stationData %>%
  mutate(tempKelvin=temp+273)