0

Can functions be applied to only certain month/year combinations of a Date field in ddply? I want to figure the mean (among other functions) by specifying month/year.

monthlySummary <- ddply(tempData, .(Date, SiteID, SubstrateID), summarize, monthlyMean=mean(Temp_C))
BgnR
  • 319
  • 1
  • 12
  • Maybe you should have look at the [xts](http://cran.r-project.org/web/packages/xts/index.html) package. – sgibb Aug 07 '13 at 17:50
  • I have looked at xts and zoo packages but I don't understand it. I'm very new to R and this forum. – BgnR Aug 07 '13 at 17:58
  • Have a look [here](http://stackoverflow.com/questions/18046184/aggregation-by-time-period-in-lubridate/18046378#18046378) – Metrics Aug 07 '13 at 20:03

1 Answers1

3

Not sure what you mean by "only certain month/year combinations" so perhaps subset is what you want, but I thought you might be asking for summarization by month. So assuming that Date is a field of class Date:

monthlySummary <- ddply(tempData, .(format(Date, "%m" ),
                        summarize, monthlyMean=mean(Temp_C))

If it's not a Date class variable, maybe you should make it one:

tempData$Date2 <- as.Date(tempData$Date, "%d/%m/%Y") # or your format

And if you wanted it by site and substrate as well as month then:

monthlySummary <- ddply(tempData,
                  .( format(Date, "%m" ), SiteID, SubstrateID), 
                   summarize, monthlyMean=mean(Temp_C))

Other date-aggregation options besides format.POSIXt do include the functions in package:lubridate and the 'yearmon' class supported in package:zoo. The exaple offered above would lump together any event occurring in January of any year. If you wanted the year-month distinction to be maintained you would only need to include that in the format-string: format(Date, "%m-%Y").

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • This is exactly what I was looking for! Thank you! I also found that I could use lubridate package. Thanks, @DWin. – BgnR Aug 20 '13 at 17:53