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...
I was looking for an answer to this question myself. GSee's answer is perfect. But because there was no data provided, I made up my own. In the process adapted ever so slightly GSee's code. To the OP, please select GSee's answer, as it answers your question. The code below is for reference if anyone is interested in this question (including my future self):
Create an xts object that begins at 8:30 and ends at 13:30 over a 3-day period:
ticks <- 24*60*10
mts <- xts( runif(ticks,0,1)
, order.by = seq( as.POSIXct("2013-01-01 08:30:00")
, as.POSIXct("2013-01-03 13:30:00")
, length = ticks
)
)
D <- split(mts, "days")
D[1]; D[2]; D[3];
GSee's Function to pull data from xts object:
PullDay <- function(x, d0 = "2013-01-01", d1 = "2013-01-03", t0 = "08:30", t1 = "13:30")
{
x[paste0(d0, " ", t0, "/", d1, " ", t1)]
}
To pull the data from 8:30 a.m. on 2013-01-02 until 1:30 p.m. on the same day, do:
PullDay(mts, d0="2013-01-02", d1="2013-01-02")
To specify exact range, do:
PullDay(mts, d0="2013-01-02", d1="2013-01-02", t0="09:00", t1="09:01")
If anyone spots an error, you have permission to edit and correct.
来源:https://stackoverflow.com/questions/9908643/how-do-i-extract-subset-day0-to-day1-index-times-from-minutely-data-via-xts