Re: [R] aggregation of a zoo object

From: jim holtman <jholtman_at_gmail.com>
Date: Tue, 29 May 2007 20:17:11 -0400

Here is one way of doing it:

> time<-c("2000-10-03 14:00:00","2000-10-03 14:10:00","2000-10-03 14:20:00",

+ "2000-10-03 15:30:00","2000-10-03 16:40:00","2000-10-03 16:50:00",
+ "2000-10-03 17:00:00","2000-10-03 17:10:00","2000-10-03 17:20:00",
+ "2000-10-03 18:30:00","2000-10-04 14:00:00","2000-10-04 14:10:00",
+ "2000-10-04 14:20:00","2000-10-04 15:30:00","2000-10-04  16:40:00",
+ "2000-10-04 16:50:00","2000-10-04 17:00:00","2000-10-04 18:30:00",
+ "2000-10-04 18:30:00","2000-10-04 18:30:00")

> # remark the last date is occuring 3 times
>
> precipitation<-c(NA,0.1,0,0,NA,0,0.2,0.3,0.5,6,7,8,9,1,0,0,NA,0,1,0)
>
> my.df <- data.frame(time=as.POSIXct(time), precip=precipitation)
> # get only good data
> my.df <- my.df[complete.cases(my.df),]
> tapply(my.df$precip, as.POSIXct(trunc(my.df$time, 'day')), sum)
2000-10-03 2000-10-04

       7.1 26.0
>

On 5/29/07, jessica.gervais_at_tudor.lu <jessica.gervais_at_tudor.lu> wrote:
>
> Dear all,
>
> I am trying to execute the following example:
>
> time<-c("2000-10-03 14:00:00","2000-10-03 14:10:00","2000-10-03
> 14:20:00","2000-10-03 15:30:00","2000-10-03 16:40:00","2000-10-03
> 16:50:00","2000-10-03 17:00:00","2000-10-03 17:10:00","2000-10-03
> 17:20:00","2000-10-03 18:30:00","2000-10-04 14:00:00","2000-10-04
> 14:10:00","2000-10-04 14:20:00","2000-10-04 15:30:00","2000-10-04
> 16:40:00","2000-10-04 16:50:00","2000-10-04 17:00:00","2000-10-04
> 18:30:00","2000-10-04 18:30:00","2000-10-04 18:30:00")
> # remark the last date is occuring 3 times
>
> precipitation<-c(NA,0.1,0,0,NA,0,0.2,0.3,0.5,6,7,8,9,1,0,0,NA,0,1,0)
>
> library(zoo)
>
> z <- zoo(precipitation, as.POSIXct(time, tz = "GMT"))
> Warning message:
> some methods for "zoo" objects do not work if the index entries in
> 'order.by' are not unique in: zoo(precipitation, as.POSIXct(time, tz =
> "GMT"))
>
> # then I want to do the sum per hour
>
> z_sum_per_hour <- aggregate(na.omit(z), function(x) as.POSIXct(trunc(x,
> "hour")),sum)
> Warning message:
> some methods for "zoo" objects do not work if the index entries in
> 'order.by' are not unique in: zoo(rval[i], x.index[i])
>
>
>
> Do anyone has an idea how to avoid that ?
>
>
>
> Thanks in advance
>
>
> Jessica
>
> ______________________________________________
> R-help_at_stat.math.ethz.ch 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.
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

	[[alternative HTML version deleted]]

______________________________________________
R-help_at_stat.math.ethz.ch 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 30 May 2007 - 00:24:50 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 Wed 30 May 2007 - 01:31:12 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.