R: ddply function applied to certain months obtained from date field

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))

Answers


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").


Need Your Help

C++ Issues with Struct array in class definition

c++ arrays class struct enums

I'm currently having a lot of issues in trying to create a struct in a class header file. In the public accessors &amp; mutators its saying that the variables are 'undeclared identifiers', as well ...