[R] how calculate mean for each group

Simon Fear Simon.Fear at synequanon.com
Thu Oct 2 17:11:53 CEST 2003


A little more succinctly and I hope also helpful:

> with(df, aggregate(list(mean=treatment),
      list(group = group, duplicate = duplicate), mean))

Note that the name of the summary of treatment is likely
best to be different from the name of the treatment
variable itself. It is tempting to do something fancy picking
up the name of the called function automatically, but
I mostly use an unnamed summary function (x) { ...}, so I won't
bother.

(BTW did I ever mention here how much I prefer with() to 
attach()/forget.to.detach() ?)

> From: Marc Schwartz [mailto:MSchwartz at medanalytics.com]
> Sent: 02 October 2003 15:37

> attach(df)
> aggregate(as.data.frame(treatment), 
>           list(group = group, duplicate = duplicate), mean)
> ### Remember to 'detach(df)'.
> 
> Doing it this way, 'treatment' retains the name attribute 
> when passed to aggregate, rather than as a vector.
 

Simon Fear
Senior Statistician
Syne qua non Ltd
Tel: +44 (0) 1379 644449
Fax: +44 (0) 1379 644445
email: Simon.Fear at synequanon.com
web: http://www.synequanon.com
 
Number of attachments included with this message: 0
 
This message (and any associated files) is confidential and\...{{dropped}}




More information about the R-help mailing list