Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个快速的问题。我想取一个系列中每 12 个元素的平均值。我有以下代码:
PPrateav = NULL for (i in 80) { PPrateav[i] = sum(PPrate[1+(i-1)*12:(i*12)])/12 }
我看不出问题出在哪里。谢谢。
这会成功的
library(zoo) PPrateav <- rollapply(PPrate, width=12, mean, by=12)
有关和论据?rollapply的更多详细信息,请参阅。widthby
?rollapply
width
by
如果您真的想使用for循环,请尝试将原始循环更改为这个新版本:
for
PPrateav <- NULL for (i in 1:floor(length(PPrate)/12)) { PPrateav[i] = mean(PPrate[(1+(i-1)*12):(i*12)]) }