[R] means of a column within groups of a data frame

John Sorkin jsorkin at grecc.umaryland.edu
Thu Dec 11 05:09:02 CET 2008


R 2.8.0
windows XP

I would like to divide the rows of data frame into five groups and then get the mean of one column within the five groups. I have accomplished this using the code below, but I hope there is an easier way, i.e. some function that I can call

# create five groups.
cut(data$BMI,5)
# get mean of AAMTCARE within each of the five groups
mean(data[data[,"BMIcuts"]=="(13.3,21.9]","AAMTCARE"])
mean(data[data[,"BMIcuts"]=="(21.9,30.5]","AAMTCARE"])
mean(data[data[,"BMIcuts"]=="(30.5,39.1]","AAMTCARE"])
mean(data[data[,"BMIcuts"]=="(39.1,47.7]","AAMTCARE"])
mean(data[data[,"BMIcuts"]=="(47.7,56.3]","AAMTCARE"]) 

As always
Thank you,
John



John David Sorkin M.D., Ph.D.
Chief, Biostatistics and Informatics
University of Maryland School of Medicine Division of Gerontology
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524
(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)

Confidentiality Statement:
This email message, including any attachments, is for th...{{dropped:6}}



More information about the R-help mailing list