[R] means of a column within groups of a data frame
Jim Lemon
jim at bitwrit.com.au
Thu Dec 11 10:20:22 CET 2008
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
More information about the R-help
mailing list