?ave

> x
       hhid h.age
1  10010020    23
2  10010020    23
3  10010126    42
4  10010126    60
5  10010142    20
6  10010142    49
7  10010142    52
8  10010150    18
9  10010150    51
10 10010150    28
> cbind(x, ave(x$h.age, x$hhid))
       hhid h.age ave(x$h.age, x$hhid)
1  10010020    23             23.00000
2  10010020    23             23.00000
3  10010126    42             51.00000
4  10010126    60             51.00000
5  10010142    20             40.33333
6  10010142    49             40.33333
7  10010142    52             40.33333
8  10010150    18             32.33333
9  10010150    51             32.33333
10 10010150    28             32.33333
>



On 6/20/06, Dieter Menne <dieter.menne@menne-biomed.de> wrote:
>
> Stephan Lindner <lindners <at> umich.edu> writes:
>
>
> > The problem is to create new variables from a data frame which
> > contains both individual and group variables, such as mean age for an
> > household. My data frame:
> >
> > df
> >
> >        hhid h.age
> > 1  10010020    23
> > 2  10010020    23
> ...
> > where hhid is the same number for each household, h.age the age for
> > each household member.
> >
> > I tried tapply, by(), and aggregate. The best I could get was:
> >
> > by(df, df$hhid, function(subset) rep(mean(subset$h.age,na.rm=T
> ),nrow(subset)))
> >
> > df$hhid: 10010020
> > [1] 23 23
> > ------------------------------------------------------------
> > df$hhid: 10010126
> > [1] 51 51
>
> try something like
>
> do.call("rbind",byresult)
>
> As you did not provide a running example, the suggestion is only
> approximately
> correct.
>
> Dieter
>
> ______________________________________________
> 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
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390 (Cell)
+1 513 247 0281 (Home)

What is the problem you are trying to solve?

	[[alternative HTML version deleted]]

