Subset xts object by time of day

后端 未结 2 1093
粉色の甜心
粉色の甜心 2020-11-30 11:00

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

相关标签:
2条回答
  • 2020-11-30 11:31

    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)
    
    0 讨论(0)
  • 2020-11-30 11:34

    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.

    0 讨论(0)
提交回复
热议问题