[R] working with zoo time index ??

Gabor Grothendieck ggrothendieck at gmail.com
Wed Jun 16 23:14:11 CEST 2010


On Wed, Jun 16, 2010 at 4:55 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> 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
>

And here it is using chron:


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)
library(chron)
z <- read.zoo(textConnection(Lines), sep = ",", FUN = as.chron)
# take mean of every 2 hour segment
aggregate(z, trunc(time(z), "02:00:00"), mean)



More information about the R-help mailing list