Re: [R] Getting the groupmean for each person

About this list Date view Thread view Subject view Author view Attachment view

From: Thomas Lumley (tlumley@u.washington.edu)
Date: Mon 10 May 2004 - 05:32:21 EST


Message-id: <Pine.A41.4.58.0405091219460.40030@homer37.u.washington.edu>

On Sat, 8 May 2004, Gabor Grothendieck wrote:

>
> predict(lm(AV~as.factor(GROUP)))

If Felix actually has a "huge" data frame this will be slow. Instead
try

groupmeans<-rowsum(AV,GROUP,reorder=FALSE)
individual.means<- groupmeans[match(GROUP, unique(GROUP)]

It uses hashing and takes roughly O(MGlogG) time for M measurements on G
groups, whereas the lm solution takes O(MG^3) [and the space requirements
are O(MG) and O(MG^2)]

Admittedly, with only 3000 observations either one will be fast enough.

        -thomas

>
>
>
> Felix Eschenburg <Atropin75 <at> t-online.de> writes:
>
> :
> : Hello list !
> :
> : I have a huge data.frame with several variables observed on about 3000
> : persons. For every person (row) there is variable called GROUP which indices
> : the group the person belongs to. There is also another variable AV for each
> : person. Now i want to create a new variable which holds the group mean of AV
> : as a value for each person.
> : With tapply(AV,GROUP,mean) i get the means for each level of GROUP, but i
> : cannot find out, how to give every person the groupmean as a value (every
> : person should have the same value as every other person in the same group).
> :
> : Has anybody any ideas how to do that ?
> :
> : Yours sincerly
> : Felix Eschenburg
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

Thomas Lumley Assoc. Professor, Biostatistics
tlumley@u.washington.edu University of Washington, Seattle

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Mon 31 May 2004 - 23:05:08 EST