[R] Create variables with common values for each group

From: Stephan Lindner <lindners_at_umich.edu>
Date: Tue 20 Jun 2006 - 18:42:56 EST


Dear all,

sorry, this is for sure really basic, but I searched a lot in the internet, and just couldn't find a solution.

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
3 10010126 42
4 10010126 60
5 10010142 20
6 10010142 49
7 10010142 52
8 10010150 18
9 10010150 51
10 10010150 28

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


df$hhid: 10010142
[1] 40.33333 40.33333 40.33333


df$hhid: 10010150
[1] 32.33333 32.33333 32.33333

Now I principally only would have to stack up the mean values, and this is where I'm stucked. The function aggregate works nice, and I could loop then, but I was wondering whether there is a better way to do that.

My end result should look like this (assigning mean.age to the data frame):

       hhid h.age  mean.age
1  10010020    23     23.00
2  10010020    23     23.00
3  10010126    42     51.00
4  10010126    60     51.00
5  10010142    20     40.33
6  10010142    49     40.33
7  10010142    52     40.33
8  10010150    18     32.33
9  10010150    51     32.33
10 10010150    28     32.33



Cheers, and thanks a lot,

Stephan Lindner

-- 
-----------------------
Stephan Lindner, Dipl.Vw.
1512 Gilbert Ct., V-17
Ann Arbor, Michigan 48105
U.S.A.
Tel.: 001-734-272-2437
E-Mail: lindners@umich.edu

"The prevailing ideas of a time were always only the ideas of the
ruling class" -- Karl Marx

______________________________________________
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 Tue Jun 20 19:58:57 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 Tue 20 Jun 2006 - 22:11:53 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.