Re: [R] weighted mean and by() with two index

2009-04-13 Thread William Dunlap
1996 1997 1998 large 0.8696908 0.121 0.6130785 0.3339162 0.4579964 medium 0.6870228 0.5291311 0.6638226 0.5130830 0.3029288 small 0.4003222 0.6453871 0.4820610 NA NA If there were a [.by then you might have to manually remove the "call" attrib

Re: [R] weighted mean and by() with two index

2009-04-13 Thread Frank E Harrell Jr
Also see the help file for the wtd.mean function in the Hmisc package, which has an example using the summarize function to do this. Frank Mike Lawrence wrote: Sounds like a job for plyr: http://had.co.nz/plyr On Mon, Apr 13, 2009 at 7:56 PM, Dong H. Oh wrote: Hi expeRts, I would like to

Re: [R] weighted mean and by() with two index

2009-04-13 Thread Mike Lawrence
Sounds like a job for plyr: http://had.co.nz/plyr On Mon, Apr 13, 2009 at 7:56 PM, Dong H. Oh wrote: > Hi expeRts, > > I would like to calculate weighted mean by two factors. > > My code is as follows: > > R> tmp <- by(re$meta.sales.lkm[, c("pc", "sales")], >                      re$meta.sales.l

[R] weighted mean and by() with two index

2009-04-13 Thread Dong H. Oh
Hi expeRts, I would like to calculate weighted mean by two factors. My code is as follows: R> tmp <- by(re$meta.sales.lkm[, c("pc", "sales")], re$meta.sales.lkm[, c("size", "yr")], function(x) weighted.mean(x[,1], x[,2])) The result is as follows: R>