If I had a data frame of 20 values:
x <- data.frame(runif(20,10,90))
And I wanted to create another column of the cumulative maximums of no more than the 5 previous values how would I do that? Basically it would be a sliding window of the 5 previous values. Below is the data of how a normal cummax would be done and a sliding cummax, I indicated where the change is apparent.
runif normal cummax sliding cummax
1 42.84205 42.84205 42.84205
2 17.17040 42.84205 42.84205
3 50.08326 50.08326 50.08326
4 21.47712 50.08326 50.08326
5 59.30754 59.30754 59.30754
6 16.49770 59.30754 59.30754
7 89.52601 89.52601 89.52601
8 49.91727 89.52601 89.52601
9 69.84386 89.52601 89.52601
10 13.92894 89.52601 89.52601
11 56.39335 89.52601 89.52601
**12 28.30264 89.52601 69.84386**
13 60.09056 89.52601 69.84386
14 17.79935 89.52601 60.09056
15 63.53476 89.52601 63.53476
16 62.98458 89.52601 63.53476
17 67.21095 89.52601 67.21095
18 16.01599 89.52601 67.21095
19 39.99181 89.52601 67.21095
20 40.96641 89.52601 67.21095
1 Answer