Re: [R] function to collapse data by factor/group

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Mon 08 May 2006 - 04:43:17 EST

On 5/7/06, Alexander Nervedi <alexnerdy@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?



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Mon May 08 04:47:53 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 09 May 2006 - 14:10:05 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.