Dear all, I have a list of precipitation record and a list of time I would like to sum them up per hour, or per day. Does such a function exist ? 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 17:10:00","2000-10-04 17:20:00","2000-10-04 18:30:00") precipitation<-c(0,0.1,0,0,0,0,0.2,0.3,0.5,6,7,8,9,1,0,0,0,0,1,0) DATA<-cbind(time,precipitation) ... ? how to sum up per hour ? Thanks in advance Jessica
We could do it using either POSIXct or chron: library(zoo) # POSIXct z <- zoo(precipitation, as.POSIXct(time, tz = "GMT")) aggregate(z, function(x) as.POSIXct(trunc(x, "hour")), sum) # chron library(chron) z <- zoo(precipitation, as.chron(as.POSIXct(time, tz = "GMT")) aggregate(z, function(x) chron(trunc(times(x), "01:00:00")), sum) On 5/24/07, jessica.gervais at tudor.lu <jessica.gervais at tudor.lu> wrote:> > Dear all, > > I have a list of precipitation record and a list of time > I would like to sum them up per hour, or per day. > Does such a function exist ? > > > 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 > 17:10:00","2000-10-04 17:20:00","2000-10-04 18:30:00") > > precipitation<-c(0,0.1,0,0,0,0,0.2,0.3,0.5,6,7,8,9,1,0,0,0,0,1,0) > > DATA<-cbind(time,precipitation) > > ... ? > how to sum up per hour ? > > 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. >
hi jessica, this should possibly do it... year <- as.data.frame(c("2000-10-03","2000-10-03","2000-10-03","2000-10-03","2000-10-03","2000-10-03" ,"2000-10-03","2000-10-04","2000-10-04","2000-10-04")) colnames(year) <- c("year") time <- as.data.frame(c("14:00:00","14:10:00","14:20:00","15:30:00","16:40:00","16:50:00", "17:00:00","17:10:00","17:20:00","18:30:00")) colnames(time) <- c("time") precipitation<-c(0,0.1,0,0,0,0,0.2,0.3,0.5,6) DATA<-(cbind(year, time ,precipitation)) tapply(precipitation, year, sum) tapply(precipitation, time, sum) --------------------------------- [[alternative HTML version deleted]]
oops error on my part...forgot that we need to get the day and hour out of the string... jessica.gervais@tudor.lu wrote: Dear all, I have a list of precipitation record and a list of time I would like to sum them up per hour, or per day. Does such a function exist ? 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 17:10:00","2000-10-04 17:20:00","2000-10-04 18:30:00") precipitation<-c(0,0.1,0,0,0,0,0.2,0.3,0.5,6,7,8,9,1,0,0,0,0,1,0) DATA<-cbind(time,precipitation) ... ? how to sum up per hour ? Thanks in advance Jessica ______________________________________________ R-help@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. --------------------------------- Sucker-punch spam with award-winning protection. [[alternative HTML version deleted]]
There was a missing ) on this one. Here it is again: library(chron) z <- zoo(precipitation, as.chron(as.POSIXct(time, tz = "GMT"))) aggregate(z, function(x) chron(trunc(times(x), "01:00:00")), sum) On 5/24/07, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> We could do it using either POSIXct or chron: > > library(zoo) > > # POSIXct > z <- zoo(precipitation, as.POSIXct(time, tz = "GMT")) > aggregate(z, function(x) as.POSIXct(trunc(x, "hour")), sum) > > # chron > library(chron) > z <- zoo(precipitation, as.chron(as.POSIXct(time, tz = "GMT")) > aggregate(z, function(x) chron(trunc(times(x), "01:00:00")), sum) > > > On 5/24/07, jessica.gervais at tudor.lu <jessica.gervais at tudor.lu> wrote: > > > > Dear all, > > > > I have a list of precipitation record and a list of time > > I would like to sum them up per hour, or per day. > > Does such a function exist ? > > > > > > 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 > > 17:10:00","2000-10-04 17:20:00","2000-10-04 18:30:00") > > > > precipitation<-c(0,0.1,0,0,0,0,0.2,0.3,0.5,6,7,8,9,1,0,0,0,0,1,0) > > > > DATA<-cbind(time,precipitation) > > > > ... ? > > how to sum up per hour ? > > > > 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. > > >
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
On Tue, 29 May 2007 jessica.gervais at tudor.lu wrote:> # 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 ?The warning does not come from the aggregate() call, but from the na.omit() call. After omitting the NAs, you have still duplicated time stamps, hence the warning is issued again. After that, aggregating works fine and produces no warnings. Z