开发者

One hour increment in R, zoo

开发者 https://www.devze.com 2023-01-14 19:02 出处:网络
How can I add one hour to all the elements of the index of a zoo series? I\'ve tried newseries <- myzooseries

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)+time开发者_如何学JAVAs("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.

0

精彩评论

暂无评论...
验证码 换一张
取 消