Re: weighted mean by week
Dimitri Liakhovitski <dimitri.liakhovitski <at> gmail.com>
2012-07-18 01:39:49 GMT
David, many thanks.
Did something get ommitted from your line?:
ddply(x ,.(myweek), summarize, m1=weighted.mean(var1,myweight),
m2=weighted.mean(var2,myweight))
Because it just reproduces x - in a somewhat different order...
Thank you!
Dimitri
On Tue, Jul 17, 2012 at 9:22 PM, David Freedman <dxf1 <at> cdc.gov> wrote:
> If there are many variables, I'd then suggest the data.table package:
>
> library(data.table)
> dt=data.table(x)
> dt[,lapply(.SD, function(x)weighted.mean(x,myweight)), keyby=c('group',
> 'myweek')]
>
> The '.SD' is an abbreviation for all the variables in the data table
> (excluding the grouping variables). There's an .SDcols= 'variables of
> interest' option if you want to limit the dozens of variables to only some
> of them. Or, in the data.table(x) statement, you could limit the created
> data table to only the variables your interested in.
>
> As an added benefit, the data.table approach is amazingly fast (particularly
> when there are numerous grouping categories)
>
>
> --
(Continue reading)