【问题标题】:split.xts at a custom timesplit.xts 在自定义时间
【发布时间】:2012-10-05 04:49:05
【问题描述】:

我目前正在使用split(xts.obj,'days') 形式的split.xts 来创建日内数据列表,以便列表的每个元素代表一天。这会将一天从午夜分解到第二天午夜(就在午夜之前)有没有办法在任意时间将数据拆分到列表中,例如说早上 9 点到第二天早上 8:59.59.999?

这是一个相当笼统的问题,但如果您想要示例数据...这里是...下面应该说明我关于午夜发生的分裂的观点。

require(xts)
x <- xts(rnorm(1000000),Sys.time()-1000000:1)
x1 <- split(x,'days')
head(x1[[2]])

编辑:

该解决方案与该问题的答案非常相似...How do I extract / subset day+0 to day+1 index times from minutely data via xts in R?,但如果有更直接的方法,将不胜感激...

【问题讨论】:

    标签: r split time-series xts


    【解决方案1】:

    这会创建一个时间向量(格林威治标准时间上午 9 点):

    as.POSIXct(as.Date( seq(range(index(x))[1], range(index(x))[2], by="days") )) + 60*60*9
    cts <- .Last.value
    xp9 <- split(x, cut(index(x), cts) )
    str(xp9)
    #List of 11
    

    【讨论】:

      【解决方案2】:

      这与your other question (?) 相同,可能的解决方案相同:

      library(xts)
      set.seed(42)
      x <- xts(rnorm(1000000), as.POSIXct('2012-10-07') - (1000000:1) )
      
      index(x) = index(x) - (9*3600)
      x1 <- lapply ( split(x,'days'), function(one_day){ index(one_day) = index(one_day) + 9*3600; one_day } )
      index(x) = index(x) + (9*3600)
      

      这里是 x:

      2012-09-25 10:13:20  1.3709584
      2012-09-25 10:13:21 -0.5646982
      2012-09-25 10:13:22  0.3631284
      ...
      2012-10-06 23:59:57  0.7505021
      2012-10-06 23:59:58 -0.4726833
      2012-10-06 23:59:59  1.1356617
      

      这里是 x1[1]:

      2012-09-25 10:13:20  1.3709584
      2012-09-25 10:13:21 -0.5646982
      2012-09-25 10:13:22  0.3631284
      ...
      2012-09-26 08:59:57 -0.7079315
      2012-09-26 08:59:58 -0.2135840
      2012-09-26 08:59:59 -1.8307128
      

      x1[[2]]:

      2012-09-26 09:00:00  2.3205603
      2012-09-26 09:00:01  1.8911404
      2012-09-26 09:00:02 -0.8547244
      ...
      2012-09-27 08:59:57 -0.5731661
      2012-09-27 08:59:58 -1.5224021
      2012-09-27 08:59:59 -0.5316183
      

      还有 x1[[12]]:

      2012-10-06 09:00:03  0.9222899
      2012-10-06 09:00:04 -0.2010127
      2012-10-06 09:00:05 -1.8403161
      ...
      2012-10-06 23:59:54 -0.5931701
      2012-10-06 23:59:55 -1.1656284
      2012-10-06 23:59:56  0.7000441
      

      如果您想要另一种方法,您也可以更改时区:

      Sys.setenv(TZ = "UTC")
      library(xts)
      set.seed(42)
      x <- xts(rnorm(1000000), as.POSIXct('2012-10-07') - (1000000:1) )
      indexTZ(x) = 'UTC+9'
      x1 <- lapply ( split(x,'days'), function(one_day){ indexTZ(one_day) = 'UTC'; one_day } )
      

      如果您上午 9 点的休息日实际上是夏令时的上午 8 点,那么您应该使用时区方法,因为 R 会自动为您进行夏令时调整。如果不是,我更喜欢以前的方法:然后我将准确描述我的意思,并且不容易受到时区数据库中的错误的影响。 (我认为 TZ 数据库在 Windows 上的工作方式也可能有所不同......我还没有机会测试它。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多