I've gotten my hands on some data I need to transform i R. The data looks like this:
df <- data.frame(time = 1:100, value = runif(100, min = -20, max = 20))
What I would like to do, is transform the data to a matrix containing running means, up to 5 time periods ahead. It's hard to explain, but an example would be like this.
Original Data
time value
1 2
2 7
3 8
4 19
5 -5
6 -15
7 4
8 6
9 12
10 20
And the result would be this matrix/data frame.
time mean-value(5) mean-value(4) mean-value(3) mean-value(2) Mean-value(1)
1 (2+7+8+19-5)/5 (2+7+8+19)/4 (2+7+8)/3 (2+7)/2 2/1
2 (7+8+19-5-15)/5 (7+8+19-5)/4 (7+8+19)/3 (7+8)/2 7/1
3 (8+19-5-15+4)/5 .....
....
....
96 na numbers/4 numbers/3 numbers/2 numbers/1
97 na na numbers/3 .....
Im am at a complete loss, I've tried some reshaping, but it doesn't get right. In the end it should also just give NA's if there is not enough time ahead observations to calculate.