如何通过 R 中的 xts 从分钟数据中提取/子集 day+0 到 day+1 索引时间? [英] How do I extract / subset day+0 to day+1 index times from minutely data via xts in R?

查看:24
本文介绍了如何通过 R 中的 xts 从分钟数据中提取/子集 day+0 到 day+1 索引时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 XTS 中生成跨日、可设置的时间序列.

例如,假设我有一个在 10 年内每天 24 小时生成的分钟时间序列 (mts).我想提取时间序列中每个天"的每个(t+0 到 13:30 t+1 的上午 08:30)时间段.

使用 xts 在同一天从 08:30 到 16:00 执行此操作是微不足道的,并且在 StackExchange 上得到了很好的解决:即 mts["T08:29:59/T16:01:00"]

但是我如何写出等价的时间序列的端点是第二天发生的时间?

非常感谢任何想法.

解决方案

这将创建一个列表,其中列表中的每个元素都是一个 xts 对象,从 8:30 开始到第二天的 13:30 结束.

D <- split(mts, "days")mts.days <- lapply(seq_along(D) - 1, function(i) {if (i > 0) rbind(D[[i]]["T08:30/T23:59:59"], D[[i + 1]]["T00:00:00/T13:30:00"])})

可以通过向列表中添加名称来扩展上述内容:

names(mts.days) <- as.Date(sapply(D, function(x) as.Date(start(x))))

然后,您可以参考 2012-01-30 上午 8:30 至下午 1:30 的数据.在 2012-01-31 像这样

mts.days[["2012-01-30"]]

或者,如果你只打算退出一天",你可以做这样的事情(遵循相同的基本逻辑)

PullDay <- function(Date="2012-01-30", t0="08:30", t1="13:30", mts=mts) {string1 <- paste(Date, " ", t0, "/", Date, " 23:59:59", sep="")string2 <- paste(as.Date(Date) + 1, " 00:00:00/", as.Date(Date) + 1, " ", t1, sep="")rbind(mts[string1], mts[string2])}

然后,PullDay("2012-01-30") 将为您提供 2012-01-30 08:30/2012-01-31 13:30 的数据子集.>

Edit2:简化为

PullDay <- function(Date="2012-01-30", t0="08:30", t1="13:30", mts=mts) {mts[paste(Date, " ", t0, "/", as.Date(Date) + 1, " ", t1, sep="")]}

这让我相信我可能仍然不明白你想要什么......

I'm trying to generate cross-day, sub-settable timeseries in XTS.

For example, let's say I have a minutely timeseries (mts) that is generated 24 hours a day over 10 years. I want to extract, say, every (08:30am on t+0 to 13:30 t+1) period for every 'day' in the timeseries.

To do this from 08:30 to, say, 16:00 on the same day with xts is trivial and well addressed on StackExchange: ie mts["T08:29:59/T16:01:00"]

But how do i write the equivalent where the endpoint of the timeseries to be subset is a time that occurs on the next day?

Any thoughts much appreciated.

解决方案

This will make a list where each element of the list is an xts object that begins at 8:30 and ends at 13:30 on the following day.

D <- split(mts, "days")
mts.days <- lapply(seq_along(D) - 1, function(i) {
  if (i > 0) rbind(D[[i]]["T08:30/T23:59:59"], D[[i + 1]]["T00:00:00/T13:30:00"])
})

Edit: The above can be extended by adding names to the list:

names(mts.days) <- as.Date(sapply(D, function(x) as.Date(start(x))))

Then, you could refer to the data from 8:30 a.m. on 2012-01-30 until 1:30 p.m. on 2012-01-31 like this

mts.days[["2012-01-30"]]

Alternatively, if you're only going to be pulling out one "day," you could do something like this (that follows the same basic logic)

PullDay <- function(Date="2012-01-30", t0="08:30", t1="13:30", mts=mts) {
  string1 <- paste(Date, " ", t0, "/", Date, " 23:59:59", sep="")
  string2 <- paste(as.Date(Date) + 1, " 00:00:00/", as.Date(Date) + 1, " ", t1, sep="")
  rbind(mts[string1], mts[string2])
}

Then, PullDay("2012-01-30") will give you the subset of data from 2012-01-30 08:30/2012-01-31 13:30.

Edit2: That simplifies to

PullDay <- function(Date="2012-01-30", t0="08:30", t1="13:30", mts=mts) {
  mts[paste(Date, " ", t0, "/", as.Date(Date) + 1, " ", t1, sep="")]
}

which leads me to believe that I may still not understand what you want...

这篇关于如何通过 R 中的 xts 从分钟数据中提取/子集 day+0 到 day+1 索引时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆