1

I have data on number of incidents in a country by year. Starting with 2000 and up through 2013, I want to calculate a 3-year rolling average of incidents. For example, I want the average number of incidents over 1998, 1999, and 2000 for a country-year "name"-2000, the average over 1999, 2000, and 2001 for "name"-2001, and so on. How should I go about doing this in R? (I'm also learning/apply the concept of rolling/moving averages for the first time, so I may be misinterpreting what I have or want to do in the first place.)

I've attached an image of what the start of my data frame looks like below (where country is "cname"). Thank you for any help!

Data frame

1 Answers1

2

Use simple moving average for yearly data

https://cran.r-project.org/web/packages/smooth/vignettes/sma.html

library(smooth)
sma(data, h=3)

or

library(TTR)
 SMA(data, n=3)
Ajay Ohri
  • 3,382
  • 3
  • 30
  • 60