BgnR
BgnR

Reputation: 319

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

Upvotes: 0

Views: 1318

Answers (1)

IRTFM
IRTFM

Reputation: 263481

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

Upvotes: 3

Related Questions