XTS分裂几天无法正常工作

时间:2018-11-02 10:21:16

标签: r xts

我尝试将xts时间序列划分为90天的窗口。我希望拆分从时间序列的第一个事件开始。

以下是一些示例数据来说明该问题:

V1 <-as.Date(c('2014-01-16','2014-01-16','2014-01-16','2014-01-16','2014-01-16', '2014-03-24','2014-03-24','2014-03-24',
       '2014-05-12', '2014-05-12', '2014-05-12', '2014-05-12', '2014-05-26', '2014-05-26'))
data <- as.data.frame(V1)
data <- xts(data,order.by = data$V1)

我希望以下命令可以完成这项工作,但是不会:

xts::split.xts(x=data,f="days",k=90)

也许这不是一个bug,这只是xts应该拆分的方式,因为它使用1970-01-01作为拆分的起点。

1 个答案:

答案 0 :(得分:0)

我使用以下功能作为解决方法:

# FUNCTION
split_xts <- function(x,units = c("mins", "hours", "days"),window_size) {

  # 'split.xts' does a good job for k = 1
  #   we use it to create some unit splits that we will aggregate
  data_split <- split.xts(x = x,f = units, k = 1)

  result <- list()
  aggregator <- NULL
  aggregator_start <- NULL
  for (s in data_split) {
    if (is.null(aggregator)) {
      aggregator <- s
      aggregator_start <- xts::first(aggregator)
      next
    }

    diff_test <-
      difftime(index(xts::first(s)),
               index(aggregator_start),
               units = units)
    # print(as.numeric(diff_test))

    # If the split is out of the authorized scope, 
    #  we close the aggregator and start a new one
    if (diff_test > window_size) {
      result[[length(result)+1]] <- aggregator
      aggregator <- s
      aggregator_start <- xts::first(aggregator)
    } else {
      aggregator <- rbind(aggregator,s)
    }

  }
  result[[length(result)+1]] <- aggregator

  return(result)
}
# TEST
data <- readRDS("xts_object_to_reproduce_split_pb.rds")
as.data.frame(data)[,"date_evt"]

V1 <-as.Date(c('2014-01-16','2014-01-16','2014-01-16','2014-01-16','2014-01-16', '2014-03-24','2014-03-24','2014-03-24',
               '2014-05-12', '2014-05-12', '2014-05-12', '2014-05-12', '2014-05-26', '2014-05-26'))
data <- as.data.frame(V1)
split_xts(data,units = "days",window_size = 30)