[R] weighted mean by week

David Freedman dxf1 at cdc.gov
Wed Jul 18 03:22:44 CEST 2012


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)


--
View this message in context: http://r.789695.n4.nabble.com/weighted-mean-by-week-tp4636814p4636825.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list