[R] Grouped weighted.mean

Erik Iverson iverson at biostat.wisc.edu
Wed May 28 16:59:25 CEST 2008


Hello -

Chip Barnaby wrote:
> Dear all --
> 
> I want to compute weighted.mean() for grouped rows.
> 
> Data frame extract is just below.  For each Key, I want the mean of IAC 
> weighted by Wt.
> 
> DP0[1:20,]
>                                                             Key 
> IAC        Wt
> 2   C3-PD030020050.PD030020050.3.12.3.0 0.765 0.8590000
> 3   C3-PD030020050.PD030020050.3.12.3.0 0.764 0.8449651
> 4   C3-PD030020050.PD030020050.3.12.3.0 0.760 0.8024975
> 5   C3-PD030020050.PD030020050.3.12.3.0 0.753 0.7326575
> 6   C3-PD030020050.PD030020050.3.12.3.0 0.743 0.6381150
> 7   C3-PD030020050.PD030020050.3.12.3.0 0.730 0.5187296
> 8   C3-PD030020050.PD030020050.3.12.3.0 0.714 0.3780000
> 65  C3-PD000000000.PD000000000.3.12.3.0 0.770 0.8590000
> 66  C3-PD000000000.PD000000000.3.12.3.0 0.770 0.8449651
> 67  C3-PD000000000.PD000000000.3.12.3.0 0.771 0.8024975
> 68  C3-PD000000000.PD000000000.3.12.3.0 0.772 0.7326575
> 69  C3-PD000000000.PD000000000.3.12.3.0 0.774 0.6381150
> 70  C3-PD000000000.PD000000000.3.12.3.0 0.777 0.5187296
> 71  C3-PD000000000.PD000000000.3.12.3.0 0.780 0.3780000
> 128 C3-PD000020000.PD000020000.3.12.3.0 0.685 0.8590000
> 129 C3-PD000020000.PD000020000.3.12.3.0 0.685 0.8449651
> 130 C3-PD000020000.PD000020000.3.12.3.0 0.684 0.8024975
> 131 C3-PD000020000.PD000020000.3.12.3.0 0.682 0.7326575
> 132 C3-PD000020000.PD000020000.3.12.3.0 0.679 0.6381150
> 133 C3-PD000020000.PD000020000.3.12.3.0 0.675 0.5187296
> 
> 
> mean -- works OK
> 
>  > MN = by( DP0$IAC, DP0$Key, mean)
> 
> 
> weighted.mean -- no go due to length of Wt
> 
>  > WMN = by( DP0$IAC, DP0$Key, weighted.mean, DP0$Wt)
> Error in FUN(data[x, ], ...) : 'x' and 'w' must have the same length
> 

Just a little confusion with how 'by' works I think, try

test <- data.frame(key = rep(c("A", "B"), each = 100),
iac = rnorm(200, 10),
wt  = runif(200))

test

by(test, test$key, function(x) weighted.mean(x$iac, x$wt))





> 
> So I tried tapply() -- same result
> 
>  > WMN = tapply( DP0, DP0$Key, function(x) weighted.mean( x$IAC, x$Wt))
> Error in tapply(DP0, DP0$Key, function(x) weighted.mean(x$IAC, x$Wt)) :
>         arguments must have same length
> 
> 
> How does one pass grouped arguments to processing functions such as 
> weighted.mean() ?
> 
> TIA,
> 
> Chip Barnaby
> 
> 
> 
> 
> 
> 
> 
> 
> ---------------------------------------------------------
> Chip Barnaby                   cbarnaby at wrightsoft.com
> Vice President of Research
> Wrightsoft Corp.               781-862-8719 x118 voice
> 131 Hartwell Ave               781-861-2058 fax
> Lexington, MA 02421         www.wrightsoft.com
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list