4

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.

n.e.w
  • 1,128
  • 10
  • 23
  • 8:30 a.m. to 1:30 p.m. on the following day, for every day, will be ALL data between 8:30 on the first day and 13:30 on the last day. If you just want to do it on, say Jan 1, 2012, it's `mts["2012-01-01 08:30/2012-01-02 13:30"]` – GSee Mar 28 '12 at 15:40
  • I'm looking to do it for that specific sub-period for all days within the 10 year series. Apologies if I didn't make that clear in my original question – n.e.w Mar 28 '12 at 15:46
  • Right, but since the subperiods overlap, you'll be left with all data between the first day at 8:30 and the last day at 13:30. – GSee Mar 28 '12 at 15:53

2 Answers2

4

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...

GSee
  • 48,880
  • 13
  • 125
  • 145
  • This is an interesting solution and cleverly put together, but the list format won't work for the analysis i want to do. Just as in `xts` I want to end up with a contiguous timeseries of that subset. Thank you very much for your attempt though; much appreciated. – n.e.w Mar 28 '12 at 19:21
  • I've provided the solution for that in 2 comments already. If you think that what you want is different than not subsetting your data at all (except for the first and last day), then please explain what you want. Do you want duplicate rownames from 8:30 a.m. until 1:30 p.m. every day? – GSee Mar 28 '12 at 19:35
  • I'm afraid I don't understand the answer you've intimated in your other two comments. I'm assuming I'm not communicating something correctly. The timeseries is not just every minute from 08:30 t0 to 13:30 t+1. It is a 24 hour timeseries. I'm just trying to pull out one defined subperiod over the two days. It could just as easily be from 09:00:00 today to 07:00:00 tomorrow. – n.e.w Mar 28 '12 at 20:11
  • I'm sorry I'm not making this clear enough. I just don't understand what you mean when you say, "8:30 a.m. to 1:30 p.m. on the following day, for every day, will be ALL data between 8:30 on the first day and 13:30 on the last day." and "but since the subperiods overlap, you'll be left with all data between the first day at 8:30 and the last day at 13:30." as this seems (to me) to be addressing a slightly different issue. – n.e.w Mar 28 '12 at 20:15
  • Ah. Ok. The confusion is that `mts["T08:30/T13:30"]` will remove all data from all days that is not between 8:30 and 1:30. It operates on several days. If you wanted a 29 hour subset of every day, you're not reducing the number of data points. I'll edit the answer now that I know that you don't want to subset every day. – GSee Mar 28 '12 at 20:34
0

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.

PatrickT
  • 10,037
  • 9
  • 76
  • 111
  • My [qmao package](https://r-forge.r-project.org/R/?group_id=1113) has some sort of related functions: [TimeOfDaySubset](https://r-forge.r-project.org/scm/viewvc.php/pkg/qmao/R/TimeOfDaySubset.R?view=markup&root=twsinstrument), [ExcludeTimes](https://r-forge.r-project.org/scm/viewvc.php/pkg/qmao/R/ExcludeTimes.R?view=markup&root=twsinstrument), [ExcludeDates](https://r-forge.r-project.org/scm/viewvc.php/pkg/qmao/R/ExcludeDates.R?view=markup&root=twsinstrument). – GSee Apr 01 '13 at 21:33
  • Thanks GSee, I'm very new to R and haven't explored much yet. I'll read up on these. Thanks! – PatrickT Apr 02 '13 at 19:39