Hi All- I have been trying to separate data into columns - specifically the date - and then aggregate the rest of the data to calculate summer hourly means. However, now I would like to calculate hourly means just over one day at a time. And, I am not able to figure out how to do this. I read some help files that I think indicated that I can only use the method below for months, years and quarters. Perhaps just separating the date into the following columns would work - Yr Mon Day Hr Min??? I originally used "zoo" and "chron" as follows for the monthly means: library(zoo) library(chron) z <- read.zoo("C:/R/SPL JJA 2006 2008.txt", header = TRUE, na.strings = -999, format = "%y%m%d%H%M", FUN = as.chron, colClasses = c("character", rep("numeric", 10))) mph <- z[months(time(z)) %in% c("Jun", "Jul", "Aug"),] mphagg <- aggregate(mph, hours, mean) Here is a sample data set: YYMMDDhhmm Precip mph Deg DegF Fuel Rel volts DegMx mphgust wm 0606010000 0.00 4.6 97.5 42.1 -999 39.5 -999 -999 5.2 0.3 0606010005 0.00 4.6 97.6 42.0 -999 38.8 -999 -999 5.7 0.3 0606010010 0.00 2.6 97.6 41.9 -999 36.9 -999 -999 3.5 0.3 0606010015 0.00 2.3 97.6 41.8 -999 37.1 -999 -999 3.7 0.3 0606010020 0.00 1.8 97.6 41.5 -999 36.3 -999 -999 1.8 0.3 0606010025 0.00 1.8 97.6 41.1 -999 36.1 -999 -999 1.8 0.3 0606010030 0.00 1.8 97.6 40.8 -999 36.1 -999 -999 1.8 0.3 0606010035 0.00 1.8 97.6 40.4 -999 35.3 -999 -999 1.8 0.3 0606010040 0.00 1.8 97.7 40.1 -999 36.0 -999 -999 1.8 0.3 0606010045 0.00 1.8 97.7 39.7 -999 36.7 -999 -999 1.8 0.3 0606010050 0.00 1.8 97.7 39.3 -999 37.3 -999 -999 1.8 0.3 0606010055 0.00 1.8 97.7 38.9 -999 38.0 -999 -999 1.8 0.3 0606010100 0.00 1.8 97.7 38.6 -999 38.9 -999 -999 1.8 0.3 Any help is appreciated- Thanks- Sherri Heck University of Colorado
This calculates hourly means:> aggregate(z, trunc(time(z), "01:00:00"), mean)Precip mph Deg DegF Fuel Rel volts DegMx mphgust wm (06/01/06 00:00:00) 0 2.375 97.625 40.8 NA 37.00833 NA NA 2.708333 0.3 (06/01/06 01:00:00) 0 1.800 97.700 38.6 NA 38.90000 NA NA 1.800000 0.3 Also note that in the examples section of help(read.zoo) is an example of aggregating as you read the data in using read.zoo. On Sun, Jun 6, 2010 at 7:40 PM, <sheck at ucar.edu> wrote:> Hi All- > > I have been trying to separate data into columns - specifically the date - > and then aggregate the rest of the data to calculate summer hourly means. > However, now I would like to calculate hourly means just over one day at a > time. ?And, I am not able to figure out how to do this. I read some help > files that I think indicated that I can only use the method below for > months, years and quarters. Perhaps just separating the date into the > following columns would work - Yr Mon Day Hr Min??? > > I originally used "zoo" and "chron" as follows for the monthly means: > > > library(zoo) > library(chron) > > z <- read.zoo("C:/R/SPL JJA 2006 2008.txt", header = TRUE, na.strings > -999, > format = "%y%m%d%H%M", FUN = as.chron, > colClasses = c("character", rep("numeric", 10))) > > mph <- z[months(time(z)) %in% c("Jun", "Jul", "Aug"),] > mphagg <- aggregate(mph, hours, mean) > > > Here is a sample data set: > > YYMMDDhhmm ? Precip ?mph ? Deg ? ?DegF ? Fuel ? Rel ? volts ? DegMx ?mphgust > wm > 0606010000 ? 0.00 ? ?4.6 ? 97.5 ? 42.1 ? -999 ? 39.5 ? -999 ? -999 ? ?5.2 > ?0.3 > 0606010005 ? 0.00 ? ?4.6 ? 97.6 ? 42.0 ? -999 ? 38.8 ? -999 ? -999 ? ?5.7 > ?0.3 > 0606010010 ? 0.00 ? ?2.6 ? 97.6 ? 41.9 ? -999 ? 36.9 ? -999 ? -999 ? ?3.5 > ?0.3 > 0606010015 ? 0.00 ? ?2.3 ? 97.6 ? 41.8 ? -999 ? 37.1 ? -999 ? -999 ? ?3.7 > ?0.3 > 0606010020 ? 0.00 ? ?1.8 ? 97.6 ? 41.5 ? -999 ? 36.3 ? -999 ? -999 ? ?1.8 > ?0.3 > 0606010025 ? 0.00 ? ?1.8 ? 97.6 ? 41.1 ? -999 ? 36.1 ? -999 ? -999 ? ?1.8 > ?0.3 > 0606010030 ? 0.00 ? ?1.8 ? 97.6 ? 40.8 ? -999 ? 36.1 ? -999 ? -999 ? ?1.8 > ?0.3 > 0606010035 ? 0.00 ? ?1.8 ? 97.6 ? 40.4 ? -999 ? 35.3 ? -999 ? -999 ? ?1.8 > ?0.3 > 0606010040 ? 0.00 ? ?1.8 ? 97.7 ? 40.1 ? -999 ? 36.0 ? -999 ? -999 ? ?1.8 > ?0.3 > 0606010045 ? 0.00 ? ?1.8 ? 97.7 ? 39.7 ? -999 ? 36.7 ? -999 ? -999 ? ?1.8 > ?0.3 > 0606010050 ? 0.00 ? ?1.8 ? 97.7 ? 39.3 ? -999 ? 37.3 ? -999 ? -999 ? ?1.8 > ?0.3 > 0606010055 ? 0.00 ? ?1.8 ? 97.7 ? 38.9 ? -999 ? 38.0 ? -999 ? -999 ? ?1.8 > ?0.3 > 0606010100 ? 0.00 ? ?1.8 ? 97.7 ? 38.6 ? -999 ? 38.9 ? -999 ? -999 ? ?1.8 > ?0.3 > > Any help is appreciated- > > Thanks- > > Sherri Heck > University of Colorado > > ______________________________________________ > 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. >
I am sure there is a more sophisticated way, but I would do it like this: hourindex <- round(as.numeric(YYMMDDHHmm)/100,digits=0) hourly.means <- aggregate(dataset,hourindex,mean) For aggregate you just need an index with the length of the amount of rows that your dataset has, that assigns a unique number to each hour. Still I am sure there are ways to extract, for example, the amount of hours since origin from a zoo object and use this as the hourindex. HTH Jannis sheck at ucar.edu schrieb:> Hi All- > > I have been trying to separate data into columns - specifically the > date - and then aggregate the rest of the data to calculate summer > hourly means. However, now I would like to calculate hourly means just > over one day at a time. And, I am not able to figure out how to do > this. I read some help files that I think indicated that I can only > use the method below for months, years and quarters. Perhaps just > separating the date into the following columns would work - Yr Mon Day > Hr Min??? > > I originally used "zoo" and "chron" as follows for the monthly means: > > > library(zoo) > library(chron) > > z <- read.zoo("C:/R/SPL JJA 2006 2008.txt", header = TRUE, na.strings > = -999, > format = "%y%m%d%H%M", FUN = as.chron, > colClasses = c("character", rep("numeric", 10))) > > mph <- z[months(time(z)) %in% c("Jun", "Jul", "Aug"),] > mphagg <- aggregate(mph, hours, mean) > > > Here is a sample data set: > > YYMMDDhhmm Precip mph Deg DegF Fuel Rel volts DegMx > mphgust wm > 0606010000 0.00 4.6 97.5 42.1 -999 39.5 -999 -999 > 5.2 0.3 > 0606010005 0.00 4.6 97.6 42.0 -999 38.8 -999 -999 > 5.7 0.3 > 0606010010 0.00 2.6 97.6 41.9 -999 36.9 -999 -999 > 3.5 0.3 > 0606010015 0.00 2.3 97.6 41.8 -999 37.1 -999 -999 > 3.7 0.3 > 0606010020 0.00 1.8 97.6 41.5 -999 36.3 -999 -999 > 1.8 0.3 > 0606010025 0.00 1.8 97.6 41.1 -999 36.1 -999 -999 > 1.8 0.3 > 0606010030 0.00 1.8 97.6 40.8 -999 36.1 -999 -999 > 1.8 0.3 > 0606010035 0.00 1.8 97.6 40.4 -999 35.3 -999 -999 > 1.8 0.3 > 0606010040 0.00 1.8 97.7 40.1 -999 36.0 -999 -999 > 1.8 0.3 > 0606010045 0.00 1.8 97.7 39.7 -999 36.7 -999 -999 > 1.8 0.3 > 0606010050 0.00 1.8 97.7 39.3 -999 37.3 -999 -999 > 1.8 0.3 > 0606010055 0.00 1.8 97.7 38.9 -999 38.0 -999 -999 > 1.8 0.3 > 0606010100 0.00 1.8 97.7 38.6 -999 38.9 -999 -999 > 1.8 0.3 > > Any help is appreciated- > > Thanks- > > Sherri Heck > University of Colorado > > ______________________________________________ > 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. >