Because these rolling functions often apply with time-series data, some of the newer and richer time-series data-handling packages already do that for you:R> library(zoo) ## load zooR> speed <- c(1,1,1,1,1,4,6,3,6,8,9)R> zsp <- zoo( speed, order.by=1:length(speed) ) ## creates a zoo objectR> rollmean(zsp, 5) ## default use 3 4 5 6 7 8 91.0 1.6 2.6 3.0 4.0 5.4 6.4R> rollmean(zsp, 5, na.pad=TRUE, align="right") ## with padding and aligned 1 2 3 4 5 6 7 8 9 10 11 NA NA NA NA 1.0 1.6 2.6 3.0 4.0 5.4 6.4R> zoo 具有出色的文档,它将向您展示更多示例,特别是如何使用真实(可能是不规则)日期执行此操作; xts 对此进行了进一步扩展,但动物园是一个更好的起点.The zoo has excellent documentation that will show you many, many more examples, in particular how to do this with real (and possibly irregular) dates; xts extends this further but zoo is a better starting point. 这篇关于避免R中的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-15 11:03