问题
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