Re: [R] working with zoo time index ??

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Wed, 16 Jun 2010 17:14:11 -0400

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@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)

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 16 Jun 2010 - 21:16:52 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 17 Jun 2010 - 02:30:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive