[R] weighted mean

Andrew C. Ward s195404 at student.uq.edu.au
Wed Nov 26 01:09:46 CET 2003


Dear Marc,

For the weighted mean, one possible solution is as follows
and will hopefully give you the general idea:

tmp <- data.frame(x=sample(1:5, 100, replace=TRUE), 
                  y=sample(1:100, 100, replace=TRUE),
                  w=runif(100))
lapply(split(tmp[, 2:3], tmp[, "x"]),
   function(x) { weighted.mean(x=x$y, w=x$w)})


Regards,

Andrew C. Ward

CAPE Centre
Department of Chemical Engineering
The University of Queensland
Brisbane Qld 4072 Australia


Quoting MZodet at ahrq.gov:

> 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.
> 
>  
> 
> 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
>




More information about the R-help mailing list