[R] weighting means
Frank E Harrell Jr
fharrell at virginia.edu
Thu Aug 22 01:16:15 CEST 2002
On Wed, 21 Aug 2002 12:10:44 -0400
kjetil halvorsen <kjetilh at umsanet.edu.bo> wrote:
> What about:
>
> > ?weighted.mean
> > ?tapply
> > Q8 <- rnorm(100)
> > Faculty <- factor(c(rep(1,50), rep(2,50)))
> > w <- 1:100
> > tapply(Q8, Faculty, weighted.mean, w)
> 1 2
> -0.0976302 -0.1457895
>
>
> Kjetil Halvorsen
>
> Tim Wilson wrote:
> >
> > Hi everyone,
> >
> > I've got a dataframe called 'faculty'. I want to do a weighted mean on
> > the column called 'Q8' weighted by the contents of column 'CETP'. In
> > addition, I need to operate on the result of splitting 'faculty'
> > according to the contents of a column 'FACULTY'. For example
> >
> > > lapply(split(faculty$Q8, faculty$FACULTY), mean)
> > $"1"
> > [1] 2.2
> >
> > $"2"
> > [1] 1.888889
> >
> > gives the mean of 'Q8' after splitting by 'FACULTY'. When I try this
> > with weighted.mean, it doesn't work because I don't know how to specify
> > the result of the split as the argument for weighted.mean. For example,
> >
> > > lapply(split(faculty$Q8, faculty$FACULTY), weighted.mean)
> > $"1"
> > [1] 2.2
> >
> > $"2"
> > [1] 1.888889
> >
> > gives the same results as above because I haven't specified the vector
> > to use for weighting. The following doesn't work at all for the obvious
> > reason that weighted.mean is not operating on the result of the split.
> >
> > > lapply(split(faculty$Q8, faculty$FACULTY), weighted.mean(faculty$Q8,
> > faculty$CETP))
> > Error in match.fun(FUN) : not function, character, or symbol:
> > "weighted.mean(faculty$Q8, faculty$CETP)"
> > >
> >
> > Any ideas?
> >
> > -Tim
> >
> > --
> > Tim Wilson | Visit Sibley online: | Check out:
> > Henry Sibley HS | http://www.isd197.org | http://www.zope.com
> > W. St. Paul, MN | | http://slashdot.org
> > wilson at visi.com | <dtml-var pithy_quote> | http://linux.com
Also take a look at the many weighted stats functions in the Hmisc library. Do
library(Hmisc)
?wtd.mean
-Frank
--
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
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list