On Wed, Jun 16, 2010 at 4:55 PM, Gabor Grothendieck
<ggrothendi...@gmail.com> wrote:
> On Wed, Jun 16, 2010 at 1:10 PM, skan <juanp...@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@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.

Reply via email to