Question

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

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top