views:

71

answers:

2

Hello

How can I add one hour to all the elements of the index of a zoo series?

I've tried

newseries <- myzooseries 
index(newseries) <- index(myzooseries)+times("1:00:00") 

but I get the message

Incompatible methods   ("Ops.dates", "Ops.times") for "+" 

thanks

My index is a chron object with date and time but I've tried with simpler examples and I can't get it

A: 

Convert to POSIXct, add 60*60 (1h in s) and then convert back.

mbq
There's no need what so ever to convert anything.
Joris Meys
Yup, I would just work with POSIXt.
mbq
@mbq : lol :-) actually, chron has some very convenient functions I miss in POSIX* , like is.weekend().
Joris Meys
`as.POSIXlt(x)$wday>5`? In general what I observe is that because of this awful date time clutter that exists in R (and not only) everybody just have her/his own favorite type and works with. I like Unix time because underlying data is solid well defined; it significantly reduces the information loss (I mean situations like "Was it CET or UTC? Maybe we should just throw it out?").
mbq
@Joris `chron` also "conveniently" does not handle time zones or daylight saving time. ;-)
Joshua Ulrich
@mbq and @Joshua : very true. What I use depends on what I have to do. chron also has the nice "feature" that it returns factors for the functions years(), months() and so on. Sometimes you want that, sometimes you don't. Colors and tastes, I guess ... ;-)
Joris Meys
Now I don't know if using POSIXct or chron.At first I used chron because I saw many examples and because I don't need the tz field. But now I think that there exist more functions that can work with POSIXct and I like its date output most.cheers
@user425895 If you like my answer, I would be grateful if you could accept it.
mbq
What happens if I add that number of seconds when the year changes and may be some leap seconds are added?
+1  A: 

This is easily solved by adding the time you want in a numerical fashion :

newseries <- myzooseries 
index(newseries) <- index(myzooseries) + 1/24

chron objects are represented as decimal numbers, so you can use that to calculate. A day is 1, so an hour is 1/24, a minute 1/1440 and so on. You can see this easily if you use the function times. This gives you the times of the object tested, eg :

> A <- chron(c("01/01/97","01/02/97","01/03/97"))

> B <- A + 1/24

> B
[1] (01/01/97 01:00:00) (01/02/97 01:00:00) (01/03/97 01:00:00)

> times(A)
Time in days:
[1] 9862 9863 9864

> times(B)
Time in days:
[1] 9862.042 9863.042 9864.042


> times(B-A)
[1] 01:00:00 01:00:00 01:00:00

> times(A[3]-B[1])
Time in days:
[1] 1.958333
Joris Meys
Hi.This is what I was doing but I was afraid of floating error introduced when using fractions.Thanks
The floating errror will make a difference of a second maybe, so I wouldn't worry about it too much.
Joris Meys
This equivalent form can also be used: x + as.numeric(times("01:00:00"))
G. Grothendieck