merge.zoo removes time zone

廉价感情. 提交于 2020-12-12 10:52:09

问题


The result of merge.zoo does not have the same time zone as its input.

Consider the following example

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

The time zone associated with zoo_merged is not really EST but

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

The time zone attribute seems to have been removed and R is probably using some sort of default timezone to display the data.

I can fix this with lubridate via

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

Is there any way to fix this properly, i.e. without having to change the timezone afterwards? I was thinking of changing the code for merge.zoo but there's not a single line of comments in the respective code...


回答1:


Solution as suggested by 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"

Or as suggested by 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"


来源:https://stackoverflow.com/questions/25269425/merge-zoo-removes-time-zone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!