我试图找到自数据开始以来的滚动均值:
samp <- structure(list(samp = c(0.25, 0.4868, 0.6043, 0.5184, -0.3572,
0.4685, 0.8264, 0.8445, 0.1331, 0.293, 0.4756, 0.866, 0.6461,
0.0401, 0.0054, 0.0744, 0.6973, 0.6304, 0.5488, 0.5468, -0.2565,
0.2557, 0.0557, 0.3347, 0.7584, 0.3547, 0.2577, 0.1238, 0.2184,
0.5956, 0.4695, 0.6517, 0.7003, 0.8364, 0.5869, 0.4403, 0.538,
0.6168, 1.0843, 1.1801, 0.617, 0.8869, 0.8073, 0.7787, 0.5705,
0.8436, 0.8202, 0.847, 0.8608, 0.8978, 0.7012, 0.8892, 0.8463,
0.7133, 0.9483, 0.9787, 0.9875, 0.8613, 0.976, 0.8893, 0.7906,
0.6472, 0.8928, 0.7855, 0.7596, 1.0218)), .Names = "samp", row.names = c(NA,
-66L), class = "data.frame")
我认为这样可行:
library(zoo)
mean <-data.frame(rollmean(samp, 1:nrow(samp)))
我试图避免写循环......
答案 0 :(得分:3)
cumsum(samp$samp)/1:nrow(samp)
答案 1 :(得分:1)
您可以使用cummean
中的dplyr
。
cummean(samp$samp)