仅在连续几天申请Delt

时间:2015-08-03 15:08:17

标签: r date quantmod

我有以下数据,我希望仅在连续几天内应用quantmod::DeltDelt(data$ch, k = 1, type = 'arithmetic')会在一个系列中返回k周期百分比差异:

  

(x2(t) - x1(t-k))/ x1(t-k)

我希望Delt在日期不连续时返回NA。这是一个可重复的例子:

data <- structure(list(Date = structure(c(15706, 15707, 15708, 15709, 
15715, 15716, 15762, 15763, 15764, 12116), class = "Date"), ch = c(12L, 
23L, 4L, 78L, 120L, 94L, 36L, 2L, 41L, 22L)), .Names = c("Date", 
"ch"), row.names = c(NA, -10L), class = "data.frame")
indx <- cumsum(c(TRUE,abs(diff(data$Date))>1))
require(quantmod)
ave(data$ch, indx, FUN=function(x) c(Delt(x),NA))
# [1]    NA  0.92 -0.83 18.50    NA -0.22    NA -0.94 19.50    NA
# Warning messages:
# 1: In `split<-.default`(`*tmp*`, g, value = lapply(split(x, g), FUN)) :
#   number of items to replace is not a multiple of replacement length
# 2: In `split<-.default`(`*tmp*`, g, value = lapply(split(x, g), FUN)) :
#   number of items to replace is not a multiple of replacement length
# 3: In `split<-.default`(`*tmp*`, g, value = lapply(split(x, g), FUN)) :
#   number of items to replace is not a multiple of replacement length
# 4: In `split<-.default`(`*tmp*`, g, value = lapply(split(x, g), FUN)) :
#   number of items to replace is not a multiple of replacement length

我想要获得的输出是:

[1]  0.92 -0.83 18.50    NA -0.22    NA -0.94 19.50    NA

我也不完全理解警告信息。建议/解决方法?

0 个答案:

没有答案