[datatable-help] stratified weighted means
David Freedman
dxf1 at cdc.gov
Wed Dec 19 18:31:35 CET 2012
I apologize for the simple question, but something that worked in v 1.8.4
fails in v 1.8.6 - it involves getting stratified weighted means from a data
table. The code is
library(data.table)
(dt <- data.table(id = rep(c(1,2), each=9),
month = rep(100L:108L, times=2),
value=rnorm(18),
swt=sample(1:18,18) ))
dt[,lapply(.SD,mean),keyby=list(id)] #fine - stratified means
dt[,lapply(.SD,function(x)weighted.mean(x,swt))] #fine - overall weighted
mean
dt[,lapply(.SD,function(x)weighted.mean(x,swt)),keyby=list(id)] #fails
'object "swt" not found'
I'm using v 1.8.6 of data.table, and I am sure that this statement for
calculating stratified weight means worked in v 1.8.4.
Output of sessionInfo is:
R version 2.15.2 (2012-10-26)
Platform: i386-w64-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United
States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] data.table_1.8.6
loaded via a namespace (and not attached):
[1] tools_2.15.2
Any help would be greatly appreciated as this is something I do almost
daily.
And I realize that I could always use something like
dt[,list(wm(month,swt),wm(value,swt)),keyby=list(id)]
But this becomes difficult if I want the stratified weighted means for a
large number of variables
thanks, david freedman
--
View this message in context: http://r.789695.n4.nabble.com/stratified-weighted-means-tp4653556.html
Sent from the datatable-help mailing list archive at Nabble.com.
More information about the datatable-help
mailing list