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-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Carl Nim
Sent: Tuesday, March 15, 2011 10:25 AM
To: r-help@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]]

______________________________________________
R-help@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.

Reply via email to