1

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

这是一个相当普遍的问题,但如果你想要样本数据......这里是......下面应该说明我关于午夜发生的分裂的观点。

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

编辑:

该解决方案与该问题的答案非常相似...如何通过 R 中的 xts 从分钟数据中提取/子集 day+0 到 day+1 索引时间?,但如果有更直接的方法,将不胜感激......

4

2 回答 2

2

这将创建一个时间向量(对于 GMT 9AM):

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
于 2012-10-05T06:32:21.590 回答
0

This is the same as your other question (?), with the same possible solution:

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)

Here is 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

Here is 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

And 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

If you want an alternative approach, you could also just change the timezone:

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 } )

If your 9am day break is actually 8am in summertime, then you should use the timezone approach, as R will do the summertime adjustments automatically for you. If not, I prefer the previous approach: I'm then describing exactly what I mean, and not vulnerable to a bug in the timezone database. (I think the TZ database might also work differently on Windows... I've not had chance to test that yet.)

于 2012-10-07T02:19:37.970 回答