Load Libraries

R Markdown

####Load Google and Tesla prices over past year (rolling 12 Month Window)

goog <- read.csv('https://raw.githubusercontent.com/jforster19/DATA607/main/GOOG.csv',colClasses=c("Date",rep("numeric",6)))
tsla <- read.csv('https://raw.githubusercontent.com/jforster19/DATA607/main/TSLA.csv',colClasses=c("Date",rep("numeric",6)))

Use window functions (in SQL or dplyr) to calculate the year-to-date average and the six-day moving averages for each item.

## # A tibble: 504 × 5
## # Groups:   symbol [2]
##    symbol Date       Close mov_avg six_day_movavg
##    <chr>  <date>     <dbl>   <dbl>          <dbl>
##  1 GOOG   2021-09-08  145.    145.            NA 
##  2 GOOG   2021-09-09  145.    145.            NA 
##  3 GOOG   2021-09-10  142.    144.            NA 
##  4 GOOG   2021-09-13  143.    144.            NA 
##  5 GOOG   2021-09-14  143.    144.            NA 
##  6 GOOG   2021-09-15  145.    144.           144.
##  7 GOOG   2021-09-16  144.    144.           144.
##  8 GOOG   2021-09-17  141.    144.           143.
##  9 GOOG   2021-09-20  139.    143.           143.
## 10 GOOG   2021-09-21  140.    143.           142.
## # … with 494 more rows

The moving average calculation uses RCppRoll to get the rolling window of time that is needed, while the cumulative ytd average is calculated with dplyr.