Re: [R] Grouped weighted.mean

From: Erik Iverson <iverson_at_biostat.wisc.edu>
Date: Wed, 28 May 2008 09:59:25 -0500

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.



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. Received on Wed 28 May 2008 - 17:16:57 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 28 May 2008 - 17:30:41 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive