I have a dataset that may contain MULTIPLE observations per date. So there could be 5 observations on date1, 2 observations on date2, and 1 observation on group3.
I want to calculate the moving average - by date - and importantly while not summarising/reducing' the number of rows. That is In this example above, I would still have 8 rows of data, and in a column next to it I would have that date's rolling average price I find this challenging because when I use a typical rolling function from ZOO package it goes line by-line and I dont know how to get it to skip by DATE
for example first step normally would be to:
df %>%
groupy_by(DATE) %>%
summarise(mean_daily_price = mean(price)) %>%
ungroup() %>%
arrange(Date) %>%
mutate( ra = rollapply(price, 2, mean, partial=T)
--- but the summarise makes me lose rows.
library(dplyr)
library(zoo)
DF = structure(list(Date = c("Jan-13", "Jan-13", "Jan-13", "Jan-13", "Jan-13", "Jul-14", "Jul-14", "Oct-16"), Price = c(100L, 200L, 300L, 1000L, 400L, 150L, 50L, 600L), Average.by.Date = c(400L, 400L, 400L, 400L, 400L, 100L, 100L, 600L), Moving_Average_Size_2 = c(NA, NA, NA, NA, NA, 250L, 250L, 350L)), .Names = c("Date", "Price", "Average.by.Date", "Moving_Average_Size_2"), class = "data.frame", row.names = c(NA,
-8L))