Hi Lao,
you tried to reinvent the wheel. Have a look at ?tapply
tapply(sleep$extra,sleep$group,mean)
Cheers
Am 16.08.2011 09:41, schrieb Lao Meng:> Hi all:
> My data:data(sleep)
>
> If I wanna calculate each group's extra,what I can do is:
> #method1
> attach(sleep)
> mean(extra[group==1])
> mean(extra[group==1])
>
>
> #method2
> result<-matrix(,0,2)
> g<-split(sleep,sleep$group)
> for(i in 1:length(g))
> {
>
result<-rbind(result,data.frame(unique(g[[i]]$group),mean(g[[i]]$extra)))
> }
> colnames(result)<-c("name","mean")
>
> But the above 2 method is a little bit tedious.Is there a "short
cut" manner
> to get the same result?
>
> Thanks a lot!
>
> My best
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
--
Eik Vettorazzi
Institut f?r Medizinische Biometrie und Epidemiologie
Universit?tsklinikum Hamburg-Eppendorf
Martinistr. 52
20246 Hamburg
T ++49/40/7410-58243
F ++49/40/7410-57790