[R] Calculating Summaries for each level of a Categorical variable

David W Freedman dxf1 at cdc.gov
Sun Jun 27 15:35:33 CEST 2010


I'd suggest using the functions within the Hmisc and plyr packages:

library(Hmisc); library(plyr)
df=data.frame(v1=rnorm(10), v2=rnorm(10),
wt=sample(1:5,rep=T),sex=rep(0:1,each=5)); df
ddply(df,~sex,summarise,v1.avg=wtd.mean(v1,wt),v2.avg=wtd.mean(v2,wt))

hope this helps, david freedman

-- 
View this message in context: http://r.789695.n4.nabble.com/Calculating-Summaries-for-each-level-of-a-Categorical-variable-tp2269349p2270001.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list