merge.zoo删除时区

时间:2014-08-12 16:26:36

标签: r merge timezone zoo posixct

merge.zoo的结果与其输入的时区不同。

考虑以下示例

library(zoo)
zoo_a=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)
          )
zoo_b=zoo(data.frame(a=1:4),
          seq(as.POSIXct("2014-01-01 00:00:01",tz="UTC"),
              as.POSIXct("2014-01-01 00:00:05",tz="UTC"),
              by=1)
          )

zoo_merged=merge(zoo_a,zoo_b)
time(zoo_merged)[1]
#2013-12-31 19:00:01 EST
time(zoo_a)[1]
#2014-01-01 00:00:01 UTC
time(zoo_b)[1]
#2014-01-01 00:00:01 UTC

zoo_merged相关联的时区不是EST,而是

library(lubridate)
tz(time(zoo_merged)[1])
#""

时区属性似乎已被删除,R可能正在使用某种默认时区来显示数据。

我可以通过<{p>}与lubridate解决此问题

time(zoo_merged)=with_tz(time(zoo_merged),tz="UTC")
time(zoo_merged)[1]
#2014-01-01 00:00:01 UTC

有没有办法妥善解决这个问题,即之后不必改变时区? 我正在考虑更改merge.zoo的代码,但相应的代码中没有一行注释......

1 个答案:

答案 0 :(得分:0)

G. Grothendieck建议的解决方案

library(xts)

merge2=function(x,y) {
  as.zoo(merge(as.xts(x), as.xts(y)))
}

time(merge2(zoo_a,zoo_b))[1]
#[1] "2014-01-01 00:00:01 UTC"

或按照42 -

的建议
merge3=function(x,y) {
  if ((tmp_tzone<-attr(time(x),"tzone"))!=attr(time(y),"tzone")) {
    message("Timezone attributes of x and y are not the same. Using default tz.")
    return(merge(x,y))
  } else {
    tmp=merge(x,y)
    attr(time(tmp),"tzone")=tmp_tzone
    return(tmp)
  }
}

#input with same tzones 
time(merge3(zoo_a,zoo_b))[1]
#[1] "2014-01-01 00:00:01 UTC"

#input with different tzones
zoo_c=zoo(data.frame(a=1:4),
          seq(as.POSIXct("2014-01-01 00:00:01",tz="EDT"),
              as.POSIXct("2014-01-01 00:00:05",tz="EDT"),
              by=1)
)

time(merge3(zoo_a,zoo_c))[1]
#Timezone attributes of x and y are not the same. Using default tz.
#[1] "2014-01-01 01:00:01 CET"