goog <- read_csv("https://raw.githubusercontent.com/jerryjerald27/Data-607/refs/heads/main/Week3Assignment/goog.csv")

head(goog)
## # A tibble: 6 × 7
##   date        open  high   low close  volume adj_close
##   <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>     <dbl>
## 1 2014-02-03 1179. 1213. 1128. 1204. 2409900     1204.
## 2 2014-01-02 1115. 1187. 1082. 1181. 2860800     1181.
## 3 2013-12-02 1064. 1121  1049. 1121. 1675400     1121.
## 4 2013-11-01 1032. 1068  1005  1060. 1372400     1060.
## 5 2013-10-01  880. 1042.  843. 1031. 2472300     1031.
## 6 2013-09-03  854.  906.  854.  876. 1742200      876.

We can call the functions with the different metrics that are available within the goog.csv

Closing price

goog <- goog %>%
  mutate(date = as.Date(date)) %>%
  arrange(date) %>%
  mutate(
    ytd_avg = cummean(close),  # Ytd average
        moving_avg_6 = rollapply(close, width = 6, FUN = mean, fill = NA, align = "right", partial = TRUE)  # 6-day moving average without NA 
  )


head(goog,10)
## # A tibble: 10 × 9
##    date        open  high   low close   volume adj_close ytd_avg moving_avg_6
##    <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>     <dbl>   <dbl>        <dbl>
##  1 2006-01-03  423.  475.  395.  433. 16055300      433.    433.         433.
##  2 2006-02-01  389.  406.  338.  363. 17734800      363.    398.         398.
##  3 2006-03-01  369.  399   332.  390  14925400      390     395.         395.
##  4 2006-04-03  390.  451.  388.  418. 10143900      418.    401.         401.
##  5 2006-05-01  418.  419.  361.  372.  8360000      372.    395.         395.
##  6 2006-06-01  374.  419.  372.  419.  6756300      419.    399.         399.
##  7 2006-07-03  420.  428.  378.  387.  6605800      387.    397.         391.
##  8 2006-08-01  385.  390   363.  379.  4779600      379.    395.         394.
##  9 2006-09-01  381.  419.  377.  402.  6243100      402.    396.         396.
## 10 2006-10-02  402.  492.  398.  476.  7490800      476.    404.         406.

Or on the highest prices

goog <- goog %>%
  mutate(date = as.Date(date)) %>%
  arrange(date) %>%
  mutate(
    ytd_avg = cummean(high),  
        moving_avg_6 = rollapply(high, width = 6, FUN = mean, fill = NA, align = "right", partial = TRUE) 
  )


head(goog,10)
## # A tibble: 10 × 9
##    date        open  high   low close   volume adj_close ytd_avg moving_avg_6
##    <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>     <dbl>   <dbl>        <dbl>
##  1 2006-01-03  423.  475.  395.  433. 16055300      433.    475.         475.
##  2 2006-02-01  389.  406.  338.  363. 17734800      363.    441.         441.
##  3 2006-03-01  369.  399   332.  390  14925400      390     427.         427.
##  4 2006-04-03  390.  451.  388.  418. 10143900      418.    433.         433.
##  5 2006-05-01  418.  419.  361.  372.  8360000      372.    430.         430.
##  6 2006-06-01  374.  419.  372.  419.  6756300      419.    428.         428.
##  7 2006-07-03  420.  428.  378.  387.  6605800      387.    428.         420.
##  8 2006-08-01  385.  390   363.  379.  4779600      379.    423.         418.
##  9 2006-09-01  381.  419.  377.  402.  6243100      402.    423.         421.
## 10 2006-10-02  402.  492.  398.  476.  7490800      476.    430.         428.