R 以不在系列中的时间对zoo系列进行细分

R 以不在系列中的时间对zoo系列进行细分,r,zoo,R,Zoo,R中是否有一个好的包,允许按时间序列中不包含的时间来子集(即索引到)时间序列? 例如,对于金融应用程序,通过不在数据库中的时间戳对价格序列进行索引时,应返回时间戳之前的最新可用价格 在代码中,这是我想要的 n =15 full.dates = seq(Sys.Date(), by = 'day', length = n) series.dates = full.dates[c(1:10, 12, 15)] require(zoo) series=zoo(rep(1,length(series.

R中是否有一个好的包,允许按时间序列中不包含的时间来子集(即索引到)时间序列? 例如,对于金融应用程序,通过不在数据库中的时间戳对价格序列进行索引时,应返回时间戳之前的最新可用价格

在代码中,这是我想要的

n =15
full.dates = seq(Sys.Date(), by = 'day', length = n)
series.dates = full.dates[c(1:10, 12, 15)] 
require(zoo)
series=zoo(rep(1,length(series.dates)), series.dates)
series[full.dates[11]]
这是回报

Data:
numeric(0)

Index:
character(0)
但是,我希望它返回full.dates[11]之前的最后一个现有日期的值,即full.dates[10]:

series[full.dates[10]]
2014-01-03 
     1 

谢谢

您可以使用
索引
来提取
动物园
对象中观察结果的索引。然后可以使用索引对对象进行子集设置。逐步显示逻辑(如果我理解正确,您只需要最后一步):

如果您希望将未完成序列中缺少的值替换为“下一个观测值向后移动”,则需要将序列与包含所需连续日期范围的“虚拟”zoo对象合并

series3 <- merge(series, zoo(, full.dates))
na.locf(series3, fromLast = TRUE)

series3我强烈主张,如果所需的索引值不存在,则子集函数不应返回前一行。子集函数应返回用户请求的内容;他们不应该假设用户想要的东西与他们要求的不同

如果这是您想要的,您可以使用
If
语句相当轻松地处理它

series.subset <- series[full.dates[11]]
if(NROW(series.subset)==0) {
  # merge series with an empty zoo object
  # that contains the index value you want
  prior <- merge(series, zoo(,full.dates[11]))
  # lag *back* one period so the NA is on the prior value
  prior <- lag(prior, 1)
  # get the index value at the prior value
  prior <- index(prior)[is.na(prior)]
  # subset again
  series.subset <- series[prior]
}
series.subset
na.locf(x,xout=newdate)
似乎并不比订阅差多少,但无论如何,我们在这里定义了
“zoo”
的一个子类,名为
“zoo2”
,其中
[
使用
na.locf
。这是一个未经测试的最小实现,但可以扩展:

as.zoo2 <- function(x) UseMethod("as.zoo2")
as.zoo2.zoo <- function(x) structure(x, class = c("zoo2", setdiff(class(x), "zoo2")))
"[.zoo2" <- function(x, i, ...) {
    if (!missing(i) && inherits(i, class(index(x)))) {
        zoo:::`[.zoo`(na.locf(x, xout = i),, ...)
    } else as.zoo2(zoo:::`[.zoo`(x, i, ...))
}

as.zoo2除了@Henrik给出的答案外,还可以查看zoo的na.locf()。非常感谢,但是没有一个包已经包含了这个非常自然的功能吗?如果没有,请您建议覆盖方法[]的正确语法在zoo中包含此功能?我在包blotter中使用了zoo对象,我无法在不属于原始索引的时间索引zoo对象,这破坏了我的脚本。如果我可以将此功能添加到zoo中,我认为blotter功能(例如updatePortfolio)将开始神奇地工作(或者我希望如此)。再次非常感谢!@user3134270,我已经用另一种解决方案更新了我的答案。我认为您需要澄清“不能在不属于原始索引的时间索引zoo对象”的含义。我的第一个回答不是表明这是可能的吗?如果我忽略了一些明显的问题,请道歉。干杯。您的解决方案不是我所需要的最佳解决方案。我还有其他使用[]的包和函数(即blotter/updatePortfolio)感谢你教我基本的R语法。有人能教我一些更高级的东西吗?如何创建我自己的类(或在R中叫什么)zoo1,它派生自zoo(?),其中上述功能是[]运算符的一部分。这就是我想要的。对于[]返回数据。没有向该类的用户公开额外的语法。非常感谢。@user3134270:正如我在回答中所说的,这是一个非常糟糕的主意。你会失望的,因为创建一个zoo的子类来子集你所描述的方式可能会破坏比修复更多的东西。另外,blotter使用xts,而不是zoo。如果如果您不了解吸墨纸包是如何工作的,那么当您更改吸墨纸包的一个基本组件的行为时,您不应该期望吸墨纸包不会损坏。对于这个问题,您有两个答案。请提出一个关于您试图解决的实际问题的新问题,而不是您试图解决的问题。
series.subset <- series[full.dates[11]]
if(NROW(series.subset)==0) {
  # merge series with an empty zoo object
  # that contains the index value you want
  prior <- merge(series, zoo(,full.dates[11]))
  # lag *back* one period so the NA is on the prior value
  prior <- lag(prior, 1)
  # get the index value at the prior value
  prior <- index(prior)[is.na(prior)]
  # subset again
  series.subset <- series[prior]
}
as.zoo2 <- function(x) UseMethod("as.zoo2")
as.zoo2.zoo <- function(x) structure(x, class = c("zoo2", setdiff(class(x), "zoo2")))
"[.zoo2" <- function(x, i, ...) {
    if (!missing(i) && inherits(i, class(index(x)))) {
        zoo:::`[.zoo`(na.locf(x, xout = i),, ...)
    } else as.zoo2(zoo:::`[.zoo`(x, i, ...))
}
> series2 <- as.zoo2(series)
> series2[full.dates[11]]
2014-01-04 
         1