Hello Everyone,I think this is a very simple problem, I have been struggling with it for a few days now. I have a 10-year daily data in the following format. Date A B C D E 1978-10-22 18 20.64 0.0 0.176 -1.76 1978-10-23 15 17.06 0.4 0.147 2.52 1978-10-24 3 7.588 0.0 0.068 -6.86 1978-10-25 9 11.491 0.0 0.102 -1.01 1978-10-26 13 14.98 1.4 0.130 1.26 I want to calculate the monthly and Annual average averages of A, B, C, D, and E, for the 10 years. I tried to use the xts package to convert the data into a time series object but was not able to even change it into the time series object. Any help would be highly appreciated Thank you in advance. -- Subodh Acharya University of Florida Gainesville, FL. [[alternative HTML version deleted]]
Try this: Lines <- "Date A B C D E 1978-10-22 18 20.64 0.0 0.176 -1.76 1978-10-23 15 17.06 0.4 0.147 2.52 1978-10-24 3 7.588 0.0 0.068 -6.86 1978-10-25 9 11.491 0.0 0.102 -1.01 1978-10-26 13 14.98 1.4 0.130 1.26 " library(zoo); library(chron) z <- read.zoo(textConnection(Lines), header = TRUE, FUN = as.chron) aggregate(z, months, mean) aggregate(z, years, mean) chron was used above to take advantage of the convenient months() and years() functions that are available for it although it could be done using Date class with only slightly more complexity: library(zoo) z <- read.zoo(textConnection(Lines), header = TRUE) aggregate(z, format(time(z), "%m"), mean) aggregate(z, format(time(z), "%Y"), mean) Read the three zoo vignettes and the article on dates in R News 4/1 as well as the references to that article. On Tue, Sep 8, 2009 at 11:23 PM, Subodh Acharya<shoebodh at gmail.com> wrote:> Hello Everyone,I think this is a very simple problem, I have been struggling > with it for a few days now. > I have a 10-year daily data ?in the following format. > > > ?Date ? ? ? ? ? ? A ? ?B ? ? ? ?C ? ? ? D ? ? ? ? ? E > ?1978-10-22 ? 18 ?20.64 ? 0.0 ? 0.176 ? ? -1.76 > ?1978-10-23 ? 15 ?17.06 ? 0.4 ? 0.147 ? ? ?2.52 > ?1978-10-24 ? ?3 ? 7.588 ? 0.0 ? 0.068 ? ? -6.86 > ?1978-10-25 ? ?9 ?11.491 ? 0.0 ? 0.102 ? ?-1.01 > ?1978-10-26 ? 13 ?14.98 ? 1.4 ? ?0.130 ? ? 1.26 > > I want to calculate the monthly and Annual average averages of A, B, C, D, > and E, for the 10 years. > > I tried to use the xts package to convert the data into a time series object > but was not able to even change it into the time series object. > Any help would be highly appreciated > > Thank you in advance. > > -- > Subodh Acharya > University of Florida > Gainesville, FL. > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Just one other addition. If by monthly summary you mean a summary that has an entry for each year/month combo rather than just one entry for all Januaries, one entry for all Februaries etc. then try this which works in both the Date and chron cases: aggregate(z, as.yearmon, mean) On Tue, Sep 8, 2009 at 11:36 PM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> Try this: > > Lines <- "Date ? ? ? ? ? ? A ? ?B ? ? ? ?C ? ? ? D ? ? ? ? ? E > ?1978-10-22 ? 18 ?20.64 ? 0.0 ? 0.176 ? ? -1.76 > ?1978-10-23 ? 15 ?17.06 ? 0.4 ? 0.147 ? ? ?2.52 > ?1978-10-24 ? ?3 ? 7.588 ? 0.0 ? 0.068 ? ? -6.86 > ?1978-10-25 ? ?9 ?11.491 ? 0.0 ? 0.102 ? ?-1.01 > ?1978-10-26 ? 13 ?14.98 ? 1.4 ? ?0.130 ? ? 1.26 " > > library(zoo); library(chron) > z <- read.zoo(textConnection(Lines), header = TRUE, FUN = as.chron) > aggregate(z, months, mean) > aggregate(z, years, mean) > > chron was used above to take advantage of the convenient months() > and years() functions that are available for it although it could be > done using Date class with only slightly more complexity: > > library(zoo) > z <- read.zoo(textConnection(Lines), header = TRUE) > aggregate(z, format(time(z), "%m"), mean) > aggregate(z, format(time(z), "%Y"), mean) > > Read the three zoo vignettes and the article on dates in R News 4/1 as well > as the references to that article. > > On Tue, Sep 8, 2009 at 11:23 PM, Subodh Acharya<shoebodh at gmail.com> wrote: >> Hello Everyone,I think this is a very simple problem, I have been struggling >> with it for a few days now. >> I have a 10-year daily data ?in the following format. >> >> >> ?Date ? ? ? ? ? ? A ? ?B ? ? ? ?C ? ? ? D ? ? ? ? ? E >> ?1978-10-22 ? 18 ?20.64 ? 0.0 ? 0.176 ? ? -1.76 >> ?1978-10-23 ? 15 ?17.06 ? 0.4 ? 0.147 ? ? ?2.52 >> ?1978-10-24 ? ?3 ? 7.588 ? 0.0 ? 0.068 ? ? -6.86 >> ?1978-10-25 ? ?9 ?11.491 ? 0.0 ? 0.102 ? ?-1.01 >> ?1978-10-26 ? 13 ?14.98 ? 1.4 ? ?0.130 ? ? 1.26 >> >> I want to calculate the monthly and Annual average averages of A, B, C, D, >> and E, for the 10 years. >> >> I tried to use the xts package to convert the data into a time series object >> but was not able to even change it into the time series object. >> Any help would be highly appreciated >> >> Thank you in advance. >> >> -- >> Subodh Acharya >> University of Florida >> Gainesville, FL. >> >> ? ? ? ?[[alternative HTML version deleted]] >> >> ______________________________________________ >> 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. >> >