如果我有20個值的數據幀:滑動cummax R中
x <- data.frame(runif(20,10,90))
,我想創造無累計最大值比5個之前的值我會怎麼做,更多的另一列?基本上它會是先前5個值的滑動窗口。下面是一個正常的cummax如何完成的數據和滑動cummax,我指出了變化的明顯。從動物園包
rollapplyr(x,5,max,partial = TRUE,by.column = FALSE)
:
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
你可以用'x [1:4,2] < - cummax(x [1:4,1])替換那些' – GSee 2012-01-29 23:48:33