[R] shift/lag when merge zoo

Achim Zeileis Achim.Zeileis at wu.ac.at
Thu Apr 16 12:25:09 CEST 2009


On Wed, 15 Apr 2009, j.k wrote:

>
> Hello alltogheter,
> I have a little problem regarding merging to zoo series.
> I want to merge two zoo series to reduce the timegaps between the stamps.
> I use the following code:
>
> data.test <-
> as.POSIXct(seq(data.input01[1,1],data.input01[nrow(data.input01),1],900),tz="GMT")
> data.troughput01 <- as.zoo(data.input01$V2)
> index(data.troughput01) <- as.POSIXct(data.input01$V1,tz="GMT")
> data.output01 <-merge(data.troughput01,zoo(,data.test))
>
> They look like that:
>
> head(data.test)
> [1] "2008-01-01 00:00:00 GMT" "2008-01-01 00:15:00 GMT" "2008-01-01 00:30:00
> GMT"
> [4] "2008-01-01 00:45:00 GMT" "2008-01-01 01:00:00 GMT" "2008-01-01 01:15:00
> GMT"
>
>> head(data.troughput01)
> 2008-01-01 00:00:00 2008-01-01 00:30:00 2008-01-01 01:00:00 2008-01-01
> 01:30:00
>           12.24180            11.27340            10.30500
> 9.33654
> 2008-01-01 02:00:00 2008-01-01 02:30:00
>            8.36811             7.62456
>
>> head(data.output01)
> 2008-01-01 01:00:00 2008-01-01 01:15:00 2008-01-01 01:30:00 2008-01-01
> 01:45:00
>            12.2418                  NA             11.2734
> NA
> 2008-01-01 02:00:00 2008-01-01 02:15:00
>            10.3050                  NA
>
>
> Are there any ideas why I have a lag of one hour?

My suspicion is that the following happens:
   - You have not set the TZ environment variable and are living on a
     machine with Central European time.
   - Then setting the tz attribute is not very sticky in R. Simple
     combination c() of times will lose it.
   - as.character() does not display the tz attribute.

My machine also fulfills the first point on this list, hence:

R> tt <- strptime(c("2008-01-01 00:00:00", "2008-01-01 00:15:00"),
+    format = "%Y-%m-%d %H:%M:%S", tz = "GMT")

R> tt
[1] "2008-01-01 00:00:00 GMT" "2008-01-01 00:15:00 GMT"
R> c(tt[1], tt[2])
[1] "2008-01-01 01:00:00 CET" "2008-01-01 01:15:00 CET"

R> as.character(tt)
[1] "2008-01-01 00:00:00" "2008-01-01 00:15:00"
R> as.character(c(tt[1], tt[2]))
[1] "2008-01-01 01:00:00" "2008-01-01 01:15:00"

c() is applied in the merge() method for "zoo" objects. as.character() is 
used for printing. If you want to perform everything in "GMT", the 
simplest solution is to set "TZ".

R> Sys.setenv(TZ = "GMT")

R> tt
[1] "2008-01-01 00:00:00 GMT" "2008-01-01 00:15:00 GMT"
R> c(tt[1], tt[2])
[1] "2008-01-01 00:00:00 GMT" "2008-01-01 00:15:00 GMT"

R> as.character(tt)
[1] "2008-01-01 00:00:00" "2008-01-01 00:15:00"
R> as.character(c(tt[1], tt[2]))
[1] "2008-01-01 00:00:00" "2008-01-01 00:15:00"

See also Gabor and Thomas's help desk article in R News 4(1).

hth,
Z

> At last I fill the NAs with na.approx:
>
> data.output01 <-merge(data.troughput01,zoo(,data.test))
>
>> head(data.output01)
> 2008-01-01 01:00:00 2008-01-01 01:15:00 2008-01-01 01:30:00 2008-01-01
> 01:45:00
>           12.24180            11.75760            11.27340
> 10.78920
> 2008-01-01 02:00:00 2008-01-01 02:15:00
>           10.30500             9.82077
>>
>
> Maybe there are suggestions for other solutions of achieving the increase of
> the resolution.
>
> Thanks in advance
> Johannes
>
> -- 
> View this message in context: http://www.nabble.com/shift-lag-when-merge-zoo-tp23057867p23057867.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>




More information about the R-help mailing list