[R] timestamp shifted by hour(s) while mering zoo objects

Gabor Grothendieck ggrothendieck at gmail.com
Sun Mar 7 21:56:02 CET 2010


On my Vista system I get an error message saying that there is no such
timezone as GMT+1.

You may be better off not using time zones and just adjusting the
times yourself.  You could just use chron and avoid the entire time
zone problem in the first place.

In the first of the two approaches below we simply use GMT everywhere
in the first approach and adjust the result by one hour manually.

In the second approach we use chron which has no time zones and again
just adjust the times as we need.

We have added one hour in both cases here but you may need to add or
subtract a different number.

Sys.setenv(TZ = "GMT")

library(zoo)

data01.Lines <- "junk
00:00:00 01.01.2007, 8.0250
01:00:00 01.01.2007, 8.0167
02:00:00 01.01.2007, 10.0917
03:00:00 01.01.2007, 8.6750
04:00:00 01.01.2007, 6.3250"

data02.Lines <- "junk
00:00:00 01.01.2007, 257.58
01:00:00 01.01.2007, 239.92
02:00:00 01.01.2007, 234.00
03:00:00 01.01.2007, 220.00
04:00:00 01.01.2007, 206.92"

# POSIXct

data01 <- read.zoo(textConnection(data01.Lines), sep=",",
	format="%H:%M:%S %d.%m.%Y", tz = "GMT", strip.white=TRUE, skip=1)

data02 <- read.zoo(textConnection(data02.Lines), sep=",",
	format="%H:%M:%S %d.%m.%Y", tz = "GMT", strip.white=TRUE, skip=1)

m <- merge(data01, data02)

# add 3600 seconds to each time
time(m) <- time(m) + 3600
m

###

# chron

library(chron)

data01c <- read.zoo(textConnection(data01.Lines), sep=",",
	format="%H:%M:%S %d.%m.%Y", FUN = as.chron, strip.white=TRUE, skip=1)

data02c <- read.zoo(textConnection(data02.Lines), sep=",",
	format="%H:%M:%S %d.%m.%Y", FUN = as.chron, strip.white=TRUE, skip=1)

mc <- merge(data01c, data02c)

time(mc) <- time(mc) + as.numeric(times("01:00:00"))
mc


On Sun, Mar 7, 2010 at 3:36 PM, CHI-YU <kigiokli at gmail.com> wrote:
> Thanks Gabor,
>
> You're right. The problem comes from the environment variable TZ. I just
> tried the Sys.getenv("TZ") and it's nothing there. After I have set the
> environment variable TZ as the same as the data, let's say
> Sys.setenv(TZ="GMT+1"), the problem is gone.
>
> In order to complete the problem I've mentioned, here are the data and the
> code:
> data01.txt
> % time[GMT+1:00] Temperature[°C]
> 00:00:00 01.01.2007, 8.0250
> 01:00:00 01.01.2007, 8.0167
> 02:00:00 01.01.2007, 10.0917
> 03:00:00 01.01.2007, 8.6750
> 04:00:00 01.01.2007, 6.3250
>
> data02.txt
> % time[GMT+1:00] Conductance[µS]
> 00:00:00 01.01.2007, 257.58
> 01:00:00 01.01.2007, 239.92
> 02:00:00 01.01.2007, 234.00
> 03:00:00 01.01.2007, 220.00
> 04:00:00 01.01.2007, 206.92
>
> data01 <- read.zoo("data01.txt", sep=",", format="%H:%M:%S %d.%m.%Y",
> tz="GMT+1", strip.white=TRUE, skip=1)
>
> data02 <- read.zoo("data02.txt", sep=",", format="%H:%M:%S %d.%m.%Y",
> tz="GMT+1", strip.white=TRUE, skip=1)
>
> merge.zoo(data01, data02)
>
> Besides, thanks for your recommendation, and I'll have a check the R News
> 4/1.
>
> Regards,
> Keith
>
> Gabor Grothendieck wrote:
>>
>> Without reproducible code (that means we can copy your code from your
>> post, paste it into our session and see the same problem that you see)
>> there is not much that can be said that addresses your specific
>> situation but in terms of general advice:
>>
>> - the inappropriate use of time zones is a frequent source of errors
>> in R for the unwary and you should read R News 4/1 to find out more
>> about this.
>>
>> - if after reading that you still want to use POSIXct your best bet is
>> to set the time zone of your session to GMT and work entirely in GMT:
>> Sys.setenv(TZ = "GMT")
>>
>>
>> On Sun, Mar 7, 2010 at 12:20 PM, Keith <kigiokli at gmail.com> wrote:
>>>
>>> Dear R-users,
>>>
>>> I have two regular hourly time series data which were recorded in time
>>> zone
>>> GMT+1, and now I would like to merge them together for further analyses.
>>> Here I used zoo and merge.zoo for my purposes and everything worked fine
>>> except the timestamp shifted 2 hours after merging which bugs me a little
>>> bit. Here is the example:
>>>
>>> data01
>>> 00:00:00 01.01.2007, 8.0250
>>> 01:00:00 01.01.2007, 8.0167
>>> 02:00:00 01.01.2007, 10.0917
>>> 03:00:00 01.01.2007, 8.6750
>>> 04:00:00 01.01.2007, 6.3250
>>>
>>> data02
>>> 00:00:00 01.01.2007, 257.58
>>> 01:00:00 01.01.2007, 239.92
>>> 02:00:00 01.01.2007, 234.00
>>> 03:00:00 01.01.2007, 220.00
>>> 04:00:00 01.01.2007, 206.92
>>>
>>> which are both read into zoo object, data01 and data02, separately by
>>> setting tz = "GMT+1". However, while merging function is operated, the
>>> result is
>>>
>>> merge.zoo(data01, data02)
>>>                    data01 data02
>>> 2007-01-01 02:00:00  8.0250 257.58
>>> 2007-01-01 03:00:00  8.0167 239.92
>>> 2007-01-01 04:00:00 10.0917 234.00
>>> 2007-01-01 05:00:00  8.6750 220.00
>>> 2007-01-01 06:00:00  6.3250 206.92
>>>
>>> which is 2 hours shifted comparing to the original data. I am wondering
>>> if
>>> it's the problem of tz parameter. Hence, I re-read the data by setting tz
>>> =
>>> "GMT", and the merging result is
>>>
>>> merge.zoo(data01, data02)
>>>                    data01 data02
>>> 2007-01-01 01:00:00  8.0250 257.58
>>> 2007-01-01 02:00:00  8.0167 239.92
>>> 2007-01-01 03:00:00 10.0917 234.00
>>> 2007-01-01 04:00:00  8.6750 220.00
>>> 2007-01-01 05:00:00  6.3250 206.92
>>>
>>> which is 1 hour shifted. I only noticed this but don't know why and how
>>> to
>>> fix it. Does anyone have idea about this issue?
>>>
>>> Best regards,
>>> Keith
>>>
>>> ______________________________________________
>>> 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