Subset xts object by time of day

2019-01-07 22:20发布

问题:

A simple question: I know how to subset time series in xts for years, months and days from the help: x['2000-05/2001'] and so on.

But how can I subset my data by hours of the day? I would like to get all data between 07:00 am and 06:00 pm. I.e., I want to extract the data during business time - irrelevant of the day (I take care for weekends later on). Help has an example of the form:

.parseISO8601('T08:30/T15:00')

But this does not work in my case. Does anybody have a clue?

回答1:

If your xts object is called x then something like y <- x["T09:30/T11:00"] works for me to get a slice of the morning session, for example.



回答2:

For some reason to cut xts time of day using x["T09:30/T11:00"] is pretty slow, I use the method from R: Efficiently subsetting dataframe based on time of day and data.table time subset vs xts time subset to make a faster function with similar syntax:

cut_time_of_day <- function(x, t_str_begin, t_str_end){

    tstr_to_sec <- function(t_str){
        #"09:00:00" to sec of day
        as.numeric(as.POSIXct(paste("1970-01-01", t_str), "UTC")) %% (24*60*60)
    }

    #POSIX ignores leap second
    #sec_of_day = as.numeric(index(x)) %% (24*60*60)                                #GMT only
    sec_of_day = {lt = as.POSIXlt(index(x)); lt$hour *60*60 + lt$min*60 + lt$sec}   #handle tzone
    sec_begin  = tstr_to_sec(t_str_begin)
    sec_end    = tstr_to_sec(t_str_end)

    return(x[ sec_of_day >= sec_begin & sec_of_day <= sec_end,])
}

Test:

n = 100000
dtime <- seq(ISOdate(2001,1,1), by = 60*60, length.out = n)
attributes(dtime)$tzone <- "CET"
x = xts((1:n), order.by = dtime)

y2 <- cut_time_of_day(x,"07:00:00", "09:00:00")
y1 <- x["T07:00:00/T09:00:00"]

identical(y1,y2)