문제

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

도움이 되었습니까?

해결책

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top