[R] Calculate monthly means

KATSCHKE, ADRIAN CIV DFAS ADRIAN.KATSCHKE at DFAS.MIL
Tue Mar 15 18:34:46 CET 2011


You could use the by() function after a little data manipulation.

The first line will create a field just of the date portion of your datetime field. Then you can use the by() function to use the indices you desire to calculate the mean.

mSamp$cDT <- chron(unlist(strsplit(as.character(mSamp$Collection_Date), split=" "))[seq(1,nrow(mSamp),2)], format="y-m-d", out.format="m/d/y")


with(mSamp, by(Value, list(years(cDT), months(cDT)), mean, na.rm=TRUE))

I like using the chron package for my dates and times.

Adrian 

FOR OFFICIAL USE ONLY.  THIS MESSAGE MAY CONTAIN PERSONNEL DATA OR INFORMATION COVERED BY THE PRIVACY ACT OF 1974.  PLEASE ENSURE THIS INFORMATION IS PROTECTED FROM UNAUTHORIZED ACCESS AND OR DISCLOSURE.


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Carl Nim
Sent: Tuesday, March 15, 2011 10:25 AM
To: r-help at r-project.org
Subject: [R] Calculate monthly means

I am trying to calculate monthly means by year of phosphates and nitrates from a multi year data set. Can anybody help me out with the most effective way to do this?

My data looks like this:

Collection_Date                   Test.Name              Value
2000-01-24 17:00:00            Phosphate               0.108
2000-01-24 17:00:00            Nitrate                     0.037
2001-11-12 08:45:00            Phosphate               0.45
...


Thanks and sorry for the blatantly "newbie" question.




      
	[[alternative HTML version deleted]]



More information about the R-help mailing list