How to extract / subset of day + 0 per day + 1 time index from thin data via xts in R?

I am trying to create cross-day, custom timelines in XTS.

For example, let's say I have a tight time ( mts ) that is created 24 hours a day for 10 years. I want to extract, say, every (8:30 am t + 0 to 13:30 t + 1) period for each day in timers.

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

But how can I write an equivalent where the endpoint of a timeseries will be a subset - is it the time that happens the next day?

Any thoughts are greatly appreciated.

+4
source share
2 answers

This will make a list in which each element of the list is an xts object that starts at 8:30 and ends at 13:30 the next 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 can refer to the data from 8:30 to 2012-01-30 to 1:30 pm. on 2012-01-31 like this

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

Alternatively, if you are only going to pull out one “day”, you can do something like this (this 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 provide you with a subset of the data from 2012-01-30 08:30 / 2012-01-31 13:30.

Edit2: It simplifies

 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 makes me think that I still don't understand what you want ...

+4
source

I searched for the answer to this question myself. GSee's answer is perfect. But since no data was provided, I made my own. So little GSee code adapts in the process. At 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):

Create an xts object that starts at 8:30 and ends at 13:30 for 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 function to retrieve data from an 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)] } 

Pull the data from 8:30 to 2013-01-02 to 1:30 pm. on the same day, do:

 PullDay(mts, d0="2013-01-02", d1="2013-01-02") 

To specify the exact range, do:

 PullDay(mts, d0="2013-01-02", d1="2013-01-02", t0="09:00", t1="09:01") 

If someone sees an error, you have permission to edit and fix it.

0
source

Source: https://habr.com/ru/post/1403907/


All Articles