【问题标题】:Why does the time zone attribute get dropped when I add a new series to a zoo object为什么当我向动物园对象添加新系列时时区属性会被删除
【发布时间】:2020-10-23 20:51:58
【问题描述】:

当我向它添加新的时间序列时,我的 zoo 对象的时区属性被剥离。 例如,

library(zoo)
ZooObject=zoo(data.frame(a=1:5),
          seq(as.POSIXct("2014-01-01 00:00:01",tz="UTC"),
              as.POSIXct("2014-01-01 00:00:05",tz="UTC"),
              by=1)
)
attr(time(ZooObject),'tzone')
#"UTC"
ZooObject$b <- 2
attr(time(ZooObject),'tzone')
#NULL

创建动物园对象后正确报告时区属性,但添加第二个系列后时区消失了(默认为区域设置)。

这给我带来了麻烦,因为我后来将每小时数据汇总到每天,因此保留正确的时区很重要。 我的解决方案是不断重新设置时区属性。

attr(time(ZooObject),'tzone') <- "UTC"

这类似于merge.zoo removes time zone

有没有办法阻止时区被剥离?

【问题讨论】:

    标签: r timezone zoo


    【解决方案1】:

    避免这种行为的一个简单方法是使用 xts 而不是 zoo。如果需要,您可以在所需的转换后将对象重新分类为 zoo。

    library(xts)
    library(zoo)
    
    ZooObject=zoo(data.frame(a=1:5),
                  seq(as.POSIXct("2014-01-01 00:00:01",tz="UTC"),
                      as.POSIXct("2014-01-01 00:00:05",tz="UTC"),
                      by=1)
    )
    
    test <- as.xts(ZooObject)
    
    attr(time(test), 'tzone')
    #> [1] "UTC"
    
    test$b <- 2
    attr(time(test), 'tzone')
    #> [1] "UTC"
    
    test2 <- as.zoo(test)
    attr(time(test2), 'tzone')
    #> [1] "UTC"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-27
      • 1970-01-01
      • 1970-01-01
      • 2014-12-18
      • 2019-11-24
      • 1970-01-01
      • 2016-11-15
      相关资源
      最近更新 更多