I want to plot trailing 30 day, 90 day, and 365 day average of a number of metrics. What's the easiest way to do this?
The change proposed by Mark will be on v3.5, starting from the next development build, the new formula to average a metric for the last 30 days will be: mean(Metric_Name[[Date-30:Date]])
On further thought, my suspicion is that days with no activity are being excluded from the average rather than being counted a 0 miles. How can I include days without activity as being 0?
win <- 7 # no. of days in window
z <- zoo(GC.season.metrics()$Distance,GC.season.metrics()$time)
z0 <- zoo(, seq(start(z), end(z), "day")) # empty vector of all the days
zm <- merge(z,z0, fill=0)
index(zm) <- as.Date(index(zm)) # round to whole days
zm <- aggregate(zm,time(zm), sum) # no dup indexes allowed in zoo
ms7 <- rollapply(zm,win, sum, na.rm=TRUE,fill=NA,align = "right",partial=TRUE)
ma7 <- rollapply(zm,win, mean, na.rm=TRUE,fill=NA,align = "right",partial=TRUE)
zresult <- merge(zm,ma7,ms7)
plot(zresult,plot.type = "single", col=c("yellow","blue","red"),xlab="Date",ylab="Dist/km",main="Sliding Window Distances")