First, is the data and then the manipulations. Finally, is the current method that I am using and as of yet is producing no data. The manipulations are to create a date and then create a rolling 12-Month average.
Monthavg<-
c(20185,20186,20187,20188,20189,201810,201811,201812,20191,20192,20193,20194,20195,20196,
20197,20198,20199,201910,201911,201912,20201
,20202,20203,20204,20205,20206,20207
,20208,20209,202010,202011)
empavg<-c(2,4,6,7,8,10,12,14,16,18,20,22,24,26,28,30,32,36,36,38,40,42,44,46,48,48,50,52,52,54,56)
ces12f <- data.frame(Monthavg,empavg)
Manipulations
ces12f<- ces12f %>% mutate(year = substr(as.character(Monthavg),1,4),
month = substr(as.character(Monthavg),5,7),
date = as.Date(paste(year,month,"1",sep ="-")))
Month_ord <- order(Monthavg)
span_month=12
ces12f<-ces12f %>% mutate(ravg = zoo::rollmeanr(empavg, 12, fill = NA))
Annual difference attempt
ces12f<- ces12f%>%
group_by(Monthavg)%>%
mutate(PreviousYear=lag(ravg,12),
PreviousMonth=lag(ravg),
AnnualDifference=ravg-PreviousYear)%>%
ungroup()
The end goal would be that 202011 minus 201911 or 47.5 minus 25.17 or 22.3. The method that I use above produces nothing but NA's. Any insights as to how I can modify my existing code or simply use an entirely different method would be greatly appreciated.