Re: [R] summing values by group

From: Dimitris Rizopoulos <>
Date: Fri 25 Mar 2005 - 03:27:11 EST

you could use '?by()', e.g.,

dat <- data.frame(CAT=sample(letters[1:5], 100, TRUE), x=rnorm(100), y=rnorm(100), z=rnorm(100))
by(dat[sapply(dat, is.numeric)], dat$CAT, sum) by(dat[sapply(dat, is.numeric)], dat$CAT, mean)

I hope it helps.


Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/16/336899
Fax: +32/16/337015

> At the risk of being wacked for asking what should be obvious....
> I have a data frame with one categorical variable "CAT" and several
> numeric variables. I want to be able to get simple statistics on
> the
> numeric variables by level. For example, just as you can use table
> (CAT) to get the counts, I'd like to be able to get the means and
> sums
> by category.
> If someone could point me in the right direction, I'd appreciate it.
> I've been through the SimpleR and Using R for Data Analysis... docs
> and I'm still clueless.
> thanks for your help.
> ______________________________________________
> mailing list
> PLEASE do read the posting guide!
> mailing list PLEASE do read the posting guide! Received on Fri Mar 25 03:30:58 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:55 EST