[R] function to collapse data by factor/group

Gabor Grothendieck ggrothendieck at gmail.com
Sun May 7 20:43:17 CEST 2006


On 5/7/06, Alexander Nervedi <alexnerdy at hotmail.com> wrote:
> Hi
>
> is there an R equivalent of the Stata collpase command?
>
> suppose i have:
>
> classes <- 1:5
> schools <- letters[1:5]
> stud <- 1:10
>
> dat <- expand.grid(School = schools, Grade = classes, Student.ID = stud)
> with(dat, table(Student.ID, School))
> dat$marks <- rnorm(nrow(dat), 50, 25)
>
>
> I  want to get the mean score by school and class  while the data is at the
> school-class-student level. I could write something up but I was wondering
> if there is already something otu there.
>

library(doBy)
summaryBy(marks ~ School + Grade, dat) # by school/Grade combo

# or
library(Hmisc)
with(dat, summarize(marks, dat[,1:2], mean)) # same

# or (not quite the same)
library(Hmisc)
summary(marks ~ School + Grade, dat) # by School and separately by Grade

Also see ?tapply, ?by, colMeans?




More information about the R-help mailing list