[R] weighted mean

Markus Jäntti markus.jantti at iki.fi
Wed Nov 26 09:55:16 CET 2003


On Wed, 2003-11-26 at 01:28, MZodet at ahrq.gov wrote:
> How do I go about generating a WEIGHTED mean (and standard error) of a
> variable (e.g., expenditures) for each level of a categorical variable
> (e.g., geographic region)?  I'm looking for something comparable to PROC
> MEANS in SAS with both a class and weight statement.
> 

I asked this question a few years ago and this is the anwer I got works:

see 
http://www.r-project.org/nocvs/mail/r-help/1999/2160.html

tapply(seq(along=wage), list(Educc,Year), 
       function(i, x=wage, w=weight)  weighted.mean(x[i], w[i]))

(wage and weight are the variable of interest and weight, while Educc
and Year are the factors)

Regards,

Markus
>  
> 
> Thanks.
> 
>  
> 
> Marc
> 
>  
> 
> 
> 
> 
> 
> 
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
-- 
Markus Jäntti <markus.jantti at iki.fi>
Abo Akademi University




More information about the R-help mailing list