Message-ID: <4940DB56.8040108@bitwrit.com.au>
Date: 2008-12-11T09:20:22Z
From: Jim Lemon
Subject: means of a column within groups of a data frame
In-Reply-To: <49404C0D.91DF.00CB.0@grecc.umaryland.edu>
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