I have a data frame (morgan) of hourly river flow, river levels and wind direction and speed thus: Time hour lev.morgan lev.lock2 lev.lock1 flow direction velocity 1 2009-07-06 15:00:00 15 3.266 3.274 3.240 1710.6 180.282 4.352 2 2009-07-06 16:00:00 16 3.268 3.272 3.240 1441.8 192.338 5.496 3 2009-07-06 17:00:00 17 3.268 3.271 3.240 1300.1 202.294 2.695 4 2009-07-06 18:00:00 18 3.267 3.274 3.241 1099.1 237.161 2.035 5 2009-07-06 19:00:00 19 3.265 3.277 3.243 986.6 237.576 0.896 6 2009-07-06 20:00:00 20 3.266 3.281 3.242 1237.6 205.686 1.257 7 2009-07-06 21:00:00 21 3.267 3.280 3.242 1513.3 26.080 0.664 8 2009-07-06 22:00:00 22 3.267 3.281 3.242 1819.5 264.280 0.646 9 2009-07-06 23:00:00 23 3.267 3.281 3.242 1954.4 337.137 0.952 10 2009-07-07 00:00:00 0 3.267 3.281 3.242 1518.9 260.006 0.562 11 2009-07-07 01:00:00 1 3.267 3.281 3.242 1082.6 252.172 0.673 12 2009-07-07 02:00:00 2 3.267 3.280 3.243 1215.9 190.007 1.286 13 2009-07-07 03:00:00 3 3.267 3.279 3.244 1093.5 260.415 1.206 : : : : : : : : : : : : : : : : : : Time is of class POSIXct I wish to take daily means of the flow, levels, and wind parameters and put them into a new dataframe. I envisage doing this with the following example code: morgan$fTime <- factor(substr(as.character(morgan$Time),1,10)) dflow <- tapply(morgan[,"flow"], morgan$fTime, mean) day <- tapply(morgan[,"Time"], morgan$fTime, mean) : : daily <- as.data.frame(cbind(day,dflow, dlev.morg,dlev.lock2, ...)) daily$day <- with(daily, as.POSIXct("1970-01-01", "%Y-%m-%d", tz="Australia/Adelaide") + day) rownames(daily) <- NULL Is there a more efficient way of doing this? I am running R-2.11.0 under Windows XP Tschüß Tony Meissner Principal Scientist (Monitoring) Resources Monitoring Group Science, Monitoring and Information Division Department for Water "Imagine" © *(ph) (08) 8595 2209 *(mob) 0401 124 971 *(fax) (08) 8595 2232 * 28 Vaughan Terrace, Berri SA 5343 PO Box 240, Berri SA 5343 DX 51103 ***The information in this e-mail may be confidential and/or legally privileged. Use or disclosure of the information by anyone other than the intended recipient is prohibited and may be unlawful. If you have received this e-mail in error, please advise by return e-mail or by telephoning +61 8 8595 2209 [[alternative HTML version deleted]]
On Thu, Jul 15, 2010 at 12:52 AM, Meissner, Tony (DFW) <Tony.Meissner at sa.gov.au> wrote:> I have a data frame (morgan) of hourly river flow, river levels and wind direction and speed thus: > ? ? ? ? Time ? ? ? ? ? hour lev.morgan lev.lock2 lev.lock1 flow ? direction ?velocity > 1 ?2009-07-06 15:00:00 ? 15 ? ? ?3.266 ? ? 3.274 ? ? 3.240 1710.6 ? 180.282 ? ?4.352 > 2 ?2009-07-06 16:00:00 ? 16 ? ? ?3.268 ? ? 3.272 ? ? 3.240 1441.8 ? 192.338 ? ?5.496 > 3 ?2009-07-06 17:00:00 ? 17 ? ? ?3.268 ? ? 3.271 ? ? 3.240 1300.1 ? 202.294 ? ?2.695 > 4 ?2009-07-06 18:00:00 ? 18 ? ? ?3.267 ? ? 3.274 ? ? 3.241 1099.1 ? 237.161 ? ?2.035 > 5 ?2009-07-06 19:00:00 ? 19 ? ? ?3.265 ? ? 3.277 ? ? 3.243 ?986.6 ? 237.576 ? ?0.896 > 6 ?2009-07-06 20:00:00 ? 20 ? ? ?3.266 ? ? 3.281 ? ? 3.242 1237.6 ? 205.686 ? ?1.257 > 7 ?2009-07-06 21:00:00 ? 21 ? ? ?3.267 ? ? 3.280 ? ? 3.242 1513.3 ? ?26.080 ? ?0.664 > 8 ?2009-07-06 22:00:00 ? 22 ? ? ?3.267 ? ? 3.281 ? ? 3.242 1819.5 ? 264.280 ? ?0.646 > 9 ?2009-07-06 23:00:00 ? 23 ? ? ?3.267 ? ? 3.281 ? ? 3.242 1954.4 ? 337.137 ? ?0.952 > 10 2009-07-07 00:00:00 ? ?0 ? ? ?3.267 ? ? 3.281 ? ? 3.242 1518.9 ? 260.006 ? ?0.562 > 11 2009-07-07 01:00:00 ? ?1 ? ? ?3.267 ? ? 3.281 ? ? 3.242 1082.6 ? 252.172 ? ?0.673 > 12 2009-07-07 02:00:00 ? ?2 ? ? ?3.267 ? ? 3.280 ? ? 3.243 1215.9 ? 190.007 ? ?1.286 > 13 2009-07-07 03:00:00 ? ?3 ? ? ?3.267 ? ? 3.279 ? ? 3.244 1093.5 ? 260.415 ? ?1.206 > : ? ? ? ? : ? ? ? ? ? ? ? : ? ? ? : ? ? ? ? ? ? ? : ? ? ? ? ?: ? ? : ? ? ? ?: ? ? ? ? : > : ? ? ? ? : ? ? ? ? ? ? ? : ? ? ? : ? ? ? ? ? ? ? : ? ? ? ? ?: ? ? : ? ? ? ?: ? ? ? ? : >There are many possibilities. Here are three. #1 can be done with only the core of R. #2 produces a zoo series which seems to be the logical representation since it is, in fact, a series so its now already in the form for other series operations. See the 3 vignettes that come with zoo. with #3 its easy to take different functions (avg, count, etc.) of different columns and if you already know SQL its particularly convenient. See http://sqldf.googlecode.com # DF2 is used in #1 and #3 DF2 <- data.frame(DF, Day = as.Date(format(DF$Time))) # 1 - aggregate aggregate(cbind(flow, direction, velocity) ~ Day, DF2, mean) # 2 - zoo library(zoo) z <- read.zoo(DF, header = TRUE, tz = "GMT") aggregate(z, as.Date, mean) # 3 - sqldf library(sqldf) sqldf("select Day, avg(flow) Flow, avg(direction) Direction, avg(velocity) Velocity from DF2 group by Day")
This is one way: df<- data.frame(Time=as.POSIXct("2009-01-01", format="%Y-%m-%d") + seq(0, 60*60*24*365-1, 60*60), lev.morgan=3+runif(24*365), lev.lock2=3+runif(24*365), flow=1000+rnorm(24*365, 200), direction=runif(24*365, 0, 360), velocity=runif(24*365, 0, 10)) (df2<- aggregate(df[ , -1], list(date=as.Date(df$Time)), FUN=mean, na.rm=TRUE)) Hope this helps Allan On 15/07/10 05:52, Meissner, Tony (DFW) wrote:> I have a data frame (morgan) of hourly river flow, river levels and wind direction and speed thus: > Time hour lev.morgan lev.lock2 lev.lock1 flow direction velocity > 1 2009-07-06 15:00:00 15 3.266 3.274 3.240 1710.6 180.282 4.352 > 2 2009-07-06 16:00:00 16 3.268 3.272 3.240 1441.8 192.338 5.496 > 3 2009-07-06 17:00:00 17 3.268 3.271 3.240 1300.1 202.294 2.695 > 4 2009-07-06 18:00:00 18 3.267 3.274 3.241 1099.1 237.161 2.035 > 5 2009-07-06 19:00:00 19 3.265 3.277 3.243 986.6 237.576 0.896 > 6 2009-07-06 20:00:00 20 3.266 3.281 3.242 1237.6 205.686 1.257 > 7 2009-07-06 21:00:00 21 3.267 3.280 3.242 1513.3 26.080 0.664 > 8 2009-07-06 22:00:00 22 3.267 3.281 3.242 1819.5 264.280 0.646 > 9 2009-07-06 23:00:00 23 3.267 3.281 3.242 1954.4 337.137 0.952 > 10 2009-07-07 00:00:00 0 3.267 3.281 3.242 1518.9 260.006 0.562 > 11 2009-07-07 01:00:00 1 3.267 3.281 3.242 1082.6 252.172 0.673 > 12 2009-07-07 02:00:00 2 3.267 3.280 3.243 1215.9 190.007 1.286 > 13 2009-07-07 03:00:00 3 3.267 3.279 3.244 1093.5 260.415 1.206 > : : : : : : : : : > : : : : : : : : : > > Time is of class POSIXct > I wish to take daily means of the flow, levels, and wind parameters and put them into a new dataframe. I envisage doing this with the following example code: > > morgan$fTime<- factor(substr(as.character(morgan$Time),1,10)) > dflow<- tapply(morgan[,"flow"], morgan$fTime, mean) > day<- tapply(morgan[,"Time"], morgan$fTime, mean) > : > : > > daily<- as.data.frame(cbind(day,dflow, dlev.morg,dlev.lock2, ...)) > daily$day<- with(daily, as.POSIXct("1970-01-01", "%Y-%m-%d", tz="Australia/Adelaide") + day) > rownames(daily)<- NULL > > Is there a more efficient way of doing this? I am running R-2.11.0 under Windows XP > > Tschüß > Tony Meissner > Principal Scientist (Monitoring) > Resources Monitoring Group > Science, Monitoring and Information Division > Department for Water > "Imagine" © > *(ph) (08) 8595 2209 > *(mob) 0401 124 971 > *(fax) (08) 8595 2232 > * 28 Vaughan Terrace, Berri SA 5343 > PO Box 240, Berri SA 5343 > DX 51103 > ***The information in this e-mail may be confidential and/or legally privileged. Use or disclosure of the information by anyone other than the intended recipient is prohibited and may be unlawful. If you have received this e-mail in error, please advise by return e-mail or by telephoning +61 8 8595 2209 > > > > > [[alternative HTML version deleted]] > > > > > ______________________________________________ > 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. >[[alternative HTML version deleted]]