**From:** Aleksey Naumov (*naumov@acsu.buffalo.edu*)

**Date:** Sun 22 Jun 2003 - 10:27:27 EST

**Next message:**Spencer Graves: "Re: [R] Using weighted.mean() in aggregate()"**Previous message:**Faheem Mitha: "Re: [R] combining mathematical notation and value substitution"**Next in thread:**Spencer Graves: "Re: [R] Using weighted.mean() in aggregate()"**Reply:**Spencer Graves: "Re: [R] Using weighted.mean() in aggregate()"

Message-id: <200306212027.27339.naumov@acsu.buffalo.edu>

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

-- Aleksey Naumov GIS Analyst Center for Health and Social Research Buffalo State College______________________________________________ R-help@stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help

**Next message:**Spencer Graves: "Re: [R] Using weighted.mean() in aggregate()"**Previous message:**Faheem Mitha: "Re: [R] combining mathematical notation and value substitution"**Next in thread:**Spencer Graves: "Re: [R] Using weighted.mean() in aggregate()"**Reply:**Spencer Graves: "Re: [R] Using weighted.mean() in aggregate()"

*
This archive was generated by hypermail 2.1.3
: Tue 01 Jul 2003 - 09:12:02 EST
*