3

我正在浏览 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)
4

2 回答 2

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 <- 相同(时间(tmp),时间(tmp2))

[1] 对

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

[1] 对

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

[1] 错误

于 2011-11-25T10:06:55.863 回答
0

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

time(tmp) - time(zsec)

给出非零范围。(尽管请注意time似乎正在丢弃差异,因为它们非常小,这就是identical(zsec, tmp)返回 TRUE 的原因。)

于 2011-11-25T11:28:35.153 回答