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}}
Hi John, ?tapply #generate groups, 5 with 100 obs each group=rep(1:5,each=100) #generate xs 2*group value+random error x=group*2+rnorm(500,0,1) #get mean of x for each group #should be about Mu={2, 4, 6, 8, 10} tapply(x,group,mean) Cheers, Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Urspr?ngliche Nachricht----- Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im Auftrag von John Sorkin Gesendet: Wednesday, December 10, 2008 11:09 PM An: r-help at r-project.org Betreff: [R] means of a column within groups of a data frame 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:9}}
John Sorkin wrote:> 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"]) >Hi John, Have a look at brkdn in the prettyR package. data$BMIcuts<-cut(data$BMI,5) brkdn(BMI~BMIcuts,data) Jim