So, I have a two time-series with a monthly frequency. Monthly Brazilian federal government revenue ("receitamensal" vector) and monthly spending ("gastomensal" vector). I want to create two vectors out of those series: accumulated revenue in the last 12 months and accumulated spending in the last 12 months. So that in any given month, the value is the sum of the last 12 months. I'm using the BETS library to get my data. Right now my code is:
library(BETS)
Receitamensal <- BETS.deflate(BETS.get(7544),
BETS.GET(433),
type='perc')
Gastomensal <- BETS.deflate(BETS.get(7546),
BETS.GET(433),
type='perc')
So, what do I do?
wow that package had a lot of dependencies to install. Does the following work for you?
tail(Receitamensal,12)
tail(Gastomensal, 12)
And then simply put sum()
around each one.
EDIT:
If you are looking for a rolling sum as indicated in the comments, then you will find the rollsum()
function from the zoo
package (thank you @ulfelder) the most useful:
install.packages("zoo")
zoo::rollsum(Receitamensal, 12, fill = NA, align = "right")