[R] Using weighted.mean() in aggregate()

Frank E Harrell Jr fharrell at virginia.edu
Sun Jun 22 13:04:19 CEST 2003


On Sat, 21 Jun 2003 18:02:01 -0700
Spencer Graves <spencer.graves at pdf.com> wrote:

>  > tstdf <- data.frame(Sub =rep(1:2, 2),
> +  Length=1:4, Slope=11:14)
>  > by(tstdf, tstdf$Sub,
> +  function(x)weighted.mean(x$Slope, x$Length))
> tstdf$Sub: 1
> [1] 12.5
> ------------------------------------------------------------
> tstdf$Sub: 2
> [1] 13.33333
>  >
> Does this answer your question?
> 
> hth.  spencer graves

Here are two other ways, using the Hmisc package summarize or mApply functions, which can take a matrix as their first argument.  summarize returns a dataframe, mApply a vector

g <- function(y) wtd.mean(y[,1],y[,2])
summarize(cbind(y, wts), llist(sex,race), g, stat.name='y')
mApply(cbind(y,wts), llist(sex,race), g)

Frank Harrell

> 
> Aleksey Naumov wrote:
> > Dear R users, I have a question on using weighted.mean() while aggregating a 
> > data frame. I have a data frame with columns Sub, Length and Slope:
> > 
> > 
> >>x[1:5,]
> > 
> >   Sub   Length        Slope
> > 1   2  351.547 0.0025284969
> > 2   2  343.738 0.0025859390
> > 3   1  696.659 0.0015948968
> > 4   2 5442.338 0.0026132544
> > 5   1  209.483 0.0005304225
> > 
> > and I would like to calculate the weighted.mean of Slope, using Length as 
> > weights, for each value of Sub. The obvious way:
> > 
> > 
> >>aggregate(list(Mean.Slope=x$Slope), by=list(Sub=x$Sub), FUN=weighted.mean, 
> > 
> > w=x$Length)
> > 
> > does not work. weighted.mean() generates warnings that "longer object length 
> > is not a multiple of shorter object length in: x * w", from which I conclude 
> > that weights are not supplied as I intend, instead each subset of Sub, when 
> > passed to weighted.mean(), receives the whole x$Length as weights, which is 
> > not correct.
> > 
> > Is there an elegant way to do this, or do I have to have a loop here?
> > 
> > Thank you,
> > Aleksey
> >
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help


---
Frank E Harrell Jr              Prof. of Biostatistics & Statistics
Div. of Biostatistics & Epidem. Dept. of Health Evaluation Sciences
U. Virginia School of Medicine  http://hesweb1.med.virginia.edu/biostat




More information about the R-help mailing list