在R时间内找到第二个间隔

时间:2011-11-25 09:39:36

标签: r epoch

我正在浏览R中的一些示例代码,它获取动物园对象(时间序列)中每秒的最后一个数据。我得到了代码,但我不明白以下几行:

time(tmp) <- as.integer(time(tmp) + 1e-7) + Epoch

为什么我们将1e-7添加到时间值?在这里粘贴完整的代码。请帮助

library(zoo)
zsec <- structure(1:10, index = structure(c(1234760403.968, 1234760403.969,
            1234760403.969, 1234760405.029, 1234760405.029, 1234760405.03,
            1234760405.03, 1234760405.072, 1234760405.073, 1234760405.073
            ), class = c("POSIXt", "POSIXct"), tzone = ""), class = "zoo")

# tmp is zsec with time discretized into one second bins
tmp <- zsec
st <- start(tmp)
Epoch <- st - as.numeric(st)
time(tmp) <- as.integer(time(tmp) + 1e-7) + Epoch

# find index of last value in each one second interval
ix <- !duplicated(time(tmp), fromLast = TRUE)

2 个答案:

答案 0 :(得分:1)

它似乎没有做任何事情。如果我删除+ 1e-7术语,R告诉我,我得到相同的结构

我认为这是一个舍入项,用于纠正可能发生的某种舍入错误。

   library(zoo) 
   zsec <- structure(1:10, index = structure(c(1234760403.968, 1234760403.969,             
                                               1234760403.969, 1234760405.029, 1234760405.029, 1234760405.03,             
                                               1234760405.03, 1234760405.072, 1234760405.073, 1234760405.073             
                                              ), class = c("POSIXt", "POSIXct"), tzone = ""), class = "zoo")  

   # tmp is zsec with time discretized into one second bins 
   tmp <- zsec 
   st <- start(tmp) 
   Epoch <- st - as.numeric(st) 
   time(tmp) <- as.integer(time(tmp) + 1e-7) + Epoch  

   # find index of last value in each one second interval 
   ix <- !duplicated(time(tmp), fromLast = TRUE) 
   time(tmp)[ix]

   tmp2 <- zsec 
   st <- start(tmp2) 
   Epoch <- st - as.numeric(st) 
   time(tmp2) <- as.integer(time(tmp2)) + Epoch 


   iy <- identical(time(tmp), time(tmp2))

iy&lt; - 相同(时间(tmp),时间(tmp2))

IY

[1] TRUE

相同((as.integer(time(tmp)+ 9.99999e-1)),as.integer(time(tmp2)))

[1] TRUE

相同((as.integer(time(tmp)+ 9.999999e-1)),as.integer(time(tmp2)))

[1] FALSE

答案 1 :(得分:0)

疯狂推测,但是添加少量东西的最常见原因是避免零除问题。也许作者想要

time(tmp) - time(zsec)

给出非零范围。 (虽然注意time似乎放弃了差异,因为它们很小,这就是为什么identical(zsec, tmp)返回TRUE。)