I have a dataset that begins in year 1988 and ends in year 2020. I want to create averages for certain time intervals. For example, 5 years: 1988-1992, 1993-1997 and so on. But I want a new collumn with these averages.
For example, suppose I have this:
anos <- 1988:2020
valores <- c(15, 18, 20, NA, 25, 27, 28, NA, 32, 35, 36, 38, 40,
42, 45, 46, NA, 50, 52, 55, 56, 58, 60, NA, 65, 66,
68, 70, 72, 75, 76, 78, 80)
dataset <- data.frame(Ano = anos, Valor = valores)
I want to have this:
anos <- 1988:2020
valores <- c(15, 18, 20, NA, 25, 27, 28, NA, 32, 35, 36, 38, 40,
42, 45, 46, NA, 50, 52, 55, 56, 58, 60, NA, 65, 66,
68, 70, 72, 75, 76, 78, 80)
medias <- c(19.5, 19.5, 19.5, 19.5, 19.5,
30.5, 30.5, 30.5, 30.5, 30.5,
40.2, 40.2, 40.2, 40.2, 40.2,
50.75, 50.75, 50.75, 50.75, 50.75,
59.75, 59.75, 59.75, 59.75, 59.75,
70.2, 70.2, 70.2, 70.2, 70.2,
78, 78, 78)
dataset <- data.frame(Ano = anos, Valor = valores, Medias = medias)
There are five year averages for each time interval and the mean value repeats itself until the next five years. The last mean is an average of 3 values since the overall period is not a multiple of five.