2012-04-03 33 views
2

我有一個不規則的時間序列包含在xt和單獨的時間索引向量(端點)。 我想根據每個端點的前5秒計算每個索引點的統計數據。所以,起點應該在每個端點之前5秒。這些時期可能會重疊。等長period.apply與R中不規則間隔的端點申請

我找不到從*申請家庭做這項工作的任何功能。我怎樣才能做到這一點?我應該手動編寫循環嗎?

這裏是我謙卑的例子,黑色是xts數據,紅色是端點。我想要在每5秒鐘間隔的所有黑點上計算出每個紅點的函數結果。

overlapping intervals

回答

1

沒有一個現成的功能,要做到這一點,但它是相當容易寫你自己的。

# Example data 
library(xts) 
data(sample_matrix) 
x <- as.xts(sample_matrix) 
# Example function 
# Calculate the mean of the last 5 days of each month, returning 
# an xts object indexed at the endpoint 
myFunction <- function(i, y) xts(t(colMeans(y[(i-4):i,])), index(y)[i]) 
# Use lapply to call your function at each endpoint 
# (removing first endpoint, since it equals zero) 
do.call(rbind, lapply(endpoints(x, 'months')[-1], FUN=myFunction, y=x)) 
+0

謝謝!花了一段時間才使我的思維變得多維。沒有那個我就無法理解這個解決方案:) – gadubishe 2012-04-06 15:11:27