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.

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to