Skip to content

summing values by group

3 messages · Larry White, Dimitris Rizopoulos, Ignacio Colonna

#
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.
#
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.

Best,
Dimitris

----
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
Web: http://www.med.kuleuven.ac.be/biostat/
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "Larry White" <ljw1001 at gmail.com>
To: <R-help at stat.math.ethz.ch>
Sent: Thursday, March 24, 2005 5:12 PM
Subject: [R] summing values by group
#
Maybe aggregate() is what you are looking for?

e.g. say your data frame is called 'mydata'

sum.by.CAT<-aggregate(mydata,list(CAT),sum)

this will give you sums by CAT for all the variables in the data set and
will yield 'NA' for any character variables you may have.

Ignacio


-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Larry White
Sent: Thursday, March 24, 2005 10:12 AM
To: R-help at stat.math.ethz.ch
Subject: [R] summing values by group

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.

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html