Seattle_vs_NY

Author

Caresse Cross Beard

Approach

Use the data found using ChatGPT about weather in Seattle and New York. The data includes date and daily weather measurements such as maximum temperature, minimum temperature, precipitation, and wind. I will use slider package to get the six-day moving average

Code base

library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

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

    intersect, setdiff, setequal, union
library(tidyr)
library(slider)
weather <- read.csv("C:/Users/CARES/Rstudio/DATA 607/weather.csv")
head(weather)
  location       date precipitation temp_max temp_min wind weather
1  Seattle 2012-01-01           0.0     12.8      5.0  4.7 drizzle
2  Seattle 2012-01-02          10.9     10.6      2.8  4.5    rain
3  Seattle 2012-01-03           0.8     11.7      7.2  2.3    rain
4  Seattle 2012-01-04          20.3     12.2      5.6  4.7    rain
5  Seattle 2012-01-05           1.3      8.9      2.8  6.1    rain
6  Seattle 2012-01-06           2.5      4.4      2.2  2.2    rain
names(weather)
[1] "location"      "date"          "precipitation" "temp_max"     
[5] "temp_min"      "wind"          "weather"      

Year to date average

weather$date <- as.Date(weather$date)
class(weather$date)
[1] "Date"
weather <- weather %>%
  group_by(location, year = format(date, "%Y")) %>%
  mutate(ytd_average = cummean(temp_max)) %>%
  ungroup()
weather %>%
  filter(location == "Seattle") %>%
  select(location, date, temp_max, ytd_average)
# A tibble: 1,461 × 4
   location date       temp_max ytd_average
   <chr>    <date>        <dbl>       <dbl>
 1 Seattle  2012-01-01     12.8       12.8 
 2 Seattle  2012-01-02     10.6       11.7 
 3 Seattle  2012-01-03     11.7       11.7 
 4 Seattle  2012-01-04     12.2       11.8 
 5 Seattle  2012-01-05      8.9       11.2 
 6 Seattle  2012-01-06      4.4       10.1 
 7 Seattle  2012-01-07      7.2        9.69
 8 Seattle  2012-01-08     10          9.72
 9 Seattle  2012-01-09      9.4        9.69
10 Seattle  2012-01-10      6.1        9.33
# ℹ 1,451 more rows
weather %>%
  filter(location == "New York") %>%
  select(location, date, temp_max, ytd_average)
# A tibble: 1,461 × 4
   location date       temp_max ytd_average
   <chr>    <date>        <dbl>       <dbl>
 1 New York 2012-01-01     10         10   
 2 New York 2012-01-02     10         10   
 3 New York 2012-01-03      0.6        6.87
 4 New York 2012-01-04     -1.7        4.73
 5 New York 2012-01-05      5.6        4.9 
 6 New York 2012-01-06     12.2        6.12
 7 New York 2012-01-07     16.1        7.54
 8 New York 2012-01-08      8.9        7.71
 9 New York 2012-01-09      3.9        7.29
10 New York 2012-01-10      8.9        7.45
# ℹ 1,451 more rows

explaination

The table shows the daily maximum temperatures in New York and the year-to-date average for 2012. The temp_max column shows the highest temperature each day. The ytd_average column shows the average of the temperatures from January 1 up to that date. For example, on January 3rd, the maximum temperature was 0.6 degrees, and the average temperature for the first three days was about 6.87 degrees. Compared to Seattle, the max temperature was 11.7 degrees on January 3rd. Seattle has an average of higher temperature than New York in January.

Next, six-day moving average

weather <- weather %>%
  group_by(location) %>%
  arrange(date) %>%
  mutate(
    six_day_average = slide_dbl(
      temp_max,
      mean,
      .before = 5,
      .complete = TRUE
    )
  ) %>%
  ungroup()
weather %>%
  select(date, location, temp_max, ytd_average, six_day_average) %>%
  head(15)
# A tibble: 15 × 5
   date       location temp_max ytd_average six_day_average
   <date>     <chr>       <dbl>       <dbl>           <dbl>
 1 2012-01-01 Seattle      12.8       12.8            NA   
 2 2012-01-01 New York     10         10              NA   
 3 2012-01-02 Seattle      10.6       11.7            NA   
 4 2012-01-02 New York     10         10              NA   
 5 2012-01-03 Seattle      11.7       11.7            NA   
 6 2012-01-03 New York      0.6        6.87           NA   
 7 2012-01-04 Seattle      12.2       11.8            NA   
 8 2012-01-04 New York     -1.7        4.73           NA   
 9 2012-01-05 Seattle       8.9       11.2            NA   
10 2012-01-05 New York      5.6        4.9            NA   
11 2012-01-06 Seattle       4.4       10.1            10.1 
12 2012-01-06 New York     12.2        6.12            6.12
13 2012-01-07 Seattle       7.2        9.69            9.17
14 2012-01-07 New York     16.1        7.54            7.13
15 2012-01-08 Seattle      10          9.72            9.07
weather %>%
  filter(location == "New York") %>%
  select(location, date, six_day_average)
# A tibble: 1,461 × 3
   location date       six_day_average
   <chr>    <date>               <dbl>
 1 New York 2012-01-01           NA   
 2 New York 2012-01-02           NA   
 3 New York 2012-01-03           NA   
 4 New York 2012-01-04           NA   
 5 New York 2012-01-05           NA   
 6 New York 2012-01-06            6.12
 7 New York 2012-01-07            7.13
 8 New York 2012-01-08            6.95
 9 New York 2012-01-09            7.5 
10 New York 2012-01-10            9.27
# ℹ 1,451 more rows
weather %>%
  filter(location == "Seattle") %>%
  select(location, date, six_day_average)
# A tibble: 1,461 × 3
   location date       six_day_average
   <chr>    <date>               <dbl>
 1 Seattle  2012-01-01           NA   
 2 Seattle  2012-01-02           NA   
 3 Seattle  2012-01-03           NA   
 4 Seattle  2012-01-04           NA   
 5 Seattle  2012-01-05           NA   
 6 Seattle  2012-01-06           10.1 
 7 Seattle  2012-01-07            9.17
 8 Seattle  2012-01-08            9.07
 9 Seattle  2012-01-09            8.68
10 Seattle  2012-01-10            7.67
# ℹ 1,451 more rows

This two tables above show New York and Seattle’s six-day moving average of maximum temperatures. The first five days display NA because there are not yet six days of temperature data available to calculate the average. Starting on January 6, the average is calculated using the current day and the previous five days. For example, on January 6, the six-day average is 10.1 in Seattle and 6.11 in New York.