[R] working with zoo time index ??

Gabor Grothendieck ggrothendieck at gmail.com
Wed Jun 16 22:55:32 CEST 2010


On Wed, Jun 16, 2010 at 1:10 PM, skan <juanpide at gmail.com> wrote:
>
> I said taking the first element everyday, but that was just an example, I
> could need one every 2 hours or something more complicated such as one every
> hour if the former one was non null.

Lines <- "1990-01-01 10:00:00 ,  0.900          #  element 1
1990-01-01 10:01:00 ,  0.910          #  element 2
1990-01-01 10:03:00 ,  0.905          #  element 3
1990-01-01 10:04:00 ,  0.905          #  element 4
1990-01-01 10:05:00 ,  0.890          #  element 5
2000-12-30 20:00:00 ,  11.233        # element 3323232"

library(zoo)
z <- read.zoo(textConnection(Lines), sep = ",", tz = "")

# take mean of every 2 hour segment
aggregate(z, as.POSIXct(cut(time(z), "2 hours", include = TRUE)), mean)

For more examples, see:
?aggregate.zoo



More information about the R-help mailing list