[R] how to calculate "conditional" mean?
Terry Mu
muster at gmail.com
Sun Dec 5 18:18:50 CET 2004
Sorry, I didn't express my problem clearly. Your answers surely help,
but it's not what I meant to ask.
I'd like mean for every day in a year, i.e,
mean for January 1st, 2nd, 3rd, ....
Febuary, 1st, ..., 28th, 29th(if leap year)
...
There is only one count per day, so there is no need to calculate mean
for each date.
sorry for the confusion. I appreciate your further comment.
Terry Mu
On Sun, 5 Dec 2004 12:04:26 -0500, Terry Mu <muster at gmail.com> wrote:
> thank you,
>
> one more question:
>
> How can I list only values for given condition?
> for example, I want to see only data from febuary, i.e. month=2?
>
>
>
> On Sun, 5 Dec 2004 16:26:11 +0000 (UTC), Gabor Grothendieck
> <ggrothendieck at myway.com> wrote:
> > Terry Mu <muster <at> gmail.com> writes:
> >
> > :
> > : a data set like this:
> >
> >
> > :
> > : year month day count
> > : 2001 1 1 10
> > : 2001 1 2 11
> > : ....
> > : 2004 7 17 8
> > : ....
> > :
> > : basically it is a count of of some numbers everyday through a few years
> > :
> > : now I'd like to get the mean of the count for every day.
> > :
> > : I thought I can do this using for and ifelse,
> > : but is there a simple way to do this? I wish a function like
> > : mean(count, 'only when year, month, day are equal') could do this.
> >
> > You want to represent your dates as objects of the Date class
> > and then use tapply. (See ?as.Date, ?Date, ?tapply, ?paste, ?with.
> > Also look up ?aggregate and ?by and read about dates in R News 4/1.)
> > Assuming DF is your data frame:
> >
> > Dates <- with(DF,
> > as.Date( paste(year, month, day, sep="-") )
> > )
> > tapply(DF$count, Dates, mean)
> >
> >
> >
> > ______________________________________________
> > 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
> >
>
More information about the R-help
mailing list