Re: [R] average by group...

From: Doran, Harold <HDoran_at_air.org>
Date: Wed 31 May 2006 - 03:29:46 EST


I didn't know about ave(). What about this, though:

dat <- data.frame(score = rnorm(100), group = gl(10,10)) group.score <- with(dat, tapply(score, group, mean)) dat$group.score <- group.score[as.character(dat$group)]

Harold

> -----Original Message-----
> From: pd@pubhealth.ku.dk [mailto:pd@pubhealth.ku.dk] On
> Behalf Of Peter Dalgaard
> Sent: Tuesday, May 30, 2006 1:09 PM
> To: Doran, Harold
> Cc: r user; rhelp
> Subject: Re: [R] average by group...
>
> "Doran, Harold" <HDoran@air.org> writes:
>
> > ?tapply
>
> Nope.
>
> ?ave
>
> >
> > > -----Original Message-----
> > > From: r-help-bounces@stat.math.ethz.ch
> > > [mailto:r-help-bounces@stat.math.ethz.ch] On Behalf Of r user
> > > Sent: Tuesday, May 30, 2006 12:27 PM
> > > To: rhelp
> > > Subject: [R] average by group...
> > >
> > > I have a dataframe with 700,000 rows and 2 vectors
> > > (columns): "group" and "score".
> > >
> > > I wish to calculate a third vector of length 700000:
> ^^^^^^^^^^^^^^^^
>
> > > the average score by group. Even though the avarge value will
> > > repeat, I wish to return the average for that particular
> group for
> > > each row.
> > >
> > > (I know I can do this by calculating each group's average
> and then
> > > using the merge command, but as my calculations get more
> complex and
> > > my data set gets larger, the merge command seems to be
> fairly slow.)
> > >
> > > ______________________________________________
> > > R-help@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
> > >
> >
> > ______________________________________________
> > R-help@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
> >
>
> --
> O__ ---- Peter Dalgaard ุster Farimagsgade 5, Entr.B
> c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
> (*) \(*) -- University of Copenhagen Denmark Ph:
> (+45) 35327918
> ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX:
> (+45) 35327907
>



R-help@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 Received on Wed May 31 03:33:13 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 31 May 2006 - 04:10:22 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.