Majid Iravani
2007-Feb-01 13:11 UTC
[R] How can I calculate conditional mean in a large dataset including date data
Dear R users, I have a dataframe with two columns: first column is date data (e.g. 1/1/2000 with character format: daily data from 1/1/1970 till 31/12/2003) and second column is temperature value. Now I'd like to calculate mean for each month in a year (i.e. May 2001, June 1997) and mean for each month in all of years. As the number of days in some months is different from others I could not write appreciate command for this. Therefore I would greatly appreciate if somebody can help me in this case Thank you Majid -------------------------------------------------------------------------------- Majid Iravani PhD Student Swiss Federal Research Institute WSL Research Group of Vegetation Ecology Z?rcherstrasse 111 CH-8903 Birmensdorf Switzerland Phone: +41-1-739-2693 Fax: +41-1-739-2215 Email: Majid.iravani at wsl.ch http://www.wsl.ch/staff/majid.iravani/
Vladimir Eremeev
2007-Feb-01 14:07 UTC
[R] How can I calculate conditional mean in a large dataset including date data
>dfr<-data.frame(day=c("1/1/1970","5/1/1970","5/12/2003","31/12/2003"),temperature=c(1,-1,2,0.5))> dfrday temperature 1 1/1/1970 1.0 2 5/1/1970 -1.0 3 5/12/2003 2.0 4 31/12/2003 0.5> aggregate(dfr["temperature"],by=list(format(as.Date(dfr$day,format="%d/%m/%Y"),"%m-%Y")),mean,na.rm=TRUE)Group.1 temperature 1 01-1970 0.00 2 12-2003 1.25> aggregate(dfr["temperature"],by=list(format(as.Date(dfr$Dt,format="%d/%m/%Y"),"%m")),mean,na.rm=TRUE)Group.1 temperature 1 01 0.00 2 12 1.25 Majid Iravani wrote:> > Dear R users, > > I have a dataframe with two columns: first column is date data (e.g. > 1/1/2000 with character format: daily data from 1/1/1970 till 31/12/2003) > and second column is temperature value. Now I'd like to calculate mean for > each month in a year (i.e. May 2001, June 1997) and mean for each month in > all of years. As the number of days in some months is different from > others > I could not write appreciate command for this. Therefore I would greatly > appreciate if somebody can help me in this case > > Thank you > Majid > >-- View this message in context: http://www.nabble.com/-R--How-can-I-calculate-conditional-mean-in-a-large-dataset-including-date-data-tf3154751.html#a8748821 Sent from the R help mailing list archive at Nabble.com.
bogdan romocea
2007-Feb-01 14:12 UTC
[R] How can I calculate conditional mean in a large dataset including date data
days <- seq(as.Date("1970/1/1"), as.Date("2003/12/31"), "days") temp <- rnorm(length(days), mean=10, sd=8) tapply(temp, format(days,"%Y-%m"), mean) tapply(temp, format(days,"%b"), mean)> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Majid Iravani > Sent: Thursday, February 01, 2007 8:11 AM > To: r-help at stat.math.ethz.ch > Subject: [R] How can I calculate conditional mean in a large > dataset including date data > > Dear R users, > > I have a dataframe with two columns: first column is date data (e.g. > 1/1/2000 with character format: daily data from 1/1/1970 till > 31/12/2003) > and second column is temperature value. Now I'd like to > calculate mean for > each month in a year (i.e. May 2001, June 1997) and mean for > each month in > all of years. As the number of days in some months is > different from others > I could not write appreciate command for this. Therefore I > would greatly > appreciate if somebody can help me in this case > > Thank you > Majid > -------------------------------------------------------------- > ------------------ > Majid Iravani > PhD Student > Swiss Federal Research Institute WSL > Research Group of Vegetation Ecology > Z?rcherstrasse 111 CH-8903 Birmensdorf Switzerland > Phone: +41-1-739-2693 > Fax: +41-1-739-2215 > Email: Majid.iravani at wsl.ch > http://www.wsl.ch/staff/majid.iravani/ > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >