I have a time series data and for each 3 years I want a moving average value. I have seen the TTR
and SMA
queries, but they all indicate that the rolling mean or the moving average operation is performed on a single column row and based on that row means a new column is created with number of NA
s on top depending on k
value.
I want the dataframe to be produced just as the original data with moving average. Since my window will be 3 that means centre column and 2 adjacent columns will be used. We can dump the first and the last column just in case as there will no adjacent columns for them.
The hyopthetical data is given below:
1961 1962 1963 1964 1965 1966 1967
1 9 13 8 4 15 1 19
2 14 2 10 6 15 7 17
3 16 7 1 18 3 9 6
As some elaboration is sought here is my idea.
for 1962 <- c(9+13+8/3, 14+2+10/3, 16+7+1/3)
and so on for successive columns. The first and the last columns can have NAs
.