本文介绍了我如何找到自使用R数据开始以来的滚动平均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图找到自数据开始以来的滚动平均值:

I am trying to find the rolling mean since the beginning of the data:

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)))

我正在努力避免编写循环...

I am trying to avoid writing loops...

推荐答案

cumsum(samp$samp)/1:nrow(samp)

这篇关于我如何找到自使用R数据开始以来的滚动平均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 00:51