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 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Not sure if this is what you're looking for. Try something like by(faculty, faculty$FACULTY, function(Q8, CETP) weighted.mean(Q8, CETP)) Andy> -----Original Message----- > From: Tim Wilson [mailto:wilson at visi.com] > Sent: Tuesday, August 20, 2002 4:35 PM > To: R-help > Subject: [R] weighting means > > > 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 > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. > -.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. > _._._._._._._._._ >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (Whitehouse Station, New Jersey, USA) that may be confidential, proprietary copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please immediately return this by e-mail and then delete it. ============================================================================= -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._