Lauri Nikkinen wrote: > Hi R-users, > > I have a data.frame like this (modificated from > https://stat.ethz.ch/pipermail/r-help/2007-August/138124.html). > > y1 <- rnorm(20) + 6.8 > y2 <- rnorm(20) + (1:20*1.7 + 1) > y3 <- rnorm(20) + (1:20*6.7 + 3.7) > y <- c(y1,y2,y3) > x <- rep(1:5,12) > f <- gl(3,20, labels=paste("lev", 1:3, sep="")) > d <- data.frame(x=x,y=y, f=f) > > and this is how I can calculate mean of these levels. > > tapply(d$y, list(d$x, d$f), mean) > > But how can I calculate the mean of d$x 1 and 2 and the grand mean of d$x 1, > 2, 3, 4, 5 (within d$f) into a table?
You might like the tables produced by summary.formula() in the Hmisc package: library(Hmisc) summary(y ~ x + f, data = d, fun=mean, method="cross", overall=TRUE) UseMethod by x, f +-+ |N| |y| +-+ +---+---------+---------+---------+---------+ | x | lev1 | lev2 | lev3 | ALL | +---+---------+---------+---------+---------+ |1 | 4 | 4 | 4 |12 | | | 6.452326|15.861256|61.393455|27.902346| +---+---------+---------+---------+---------+ |2 | 4 | 4 | 4 |12 | | | 7.403041|17.296270|68.208299|30.969203| +---+---------+---------+---------+---------+ |3 | 4 | 4 | 4 |12 | | | 6.117648|17.976864|73.479837|32.524783| +---+---------+---------+---------+---------+ |4 | 4 | 4 | 4 |12 | | | 7.831390|19.696998|80.323382|35.950590| +---+---------+---------+---------+---------+ |5 | 4 | 4 | 4 |12 | | | 6.746213|21.101952|87.430087|38.426084| +---+---------+---------+---------+---------+ |ALL|20 |20 |20 |60 | | | 6.910124|18.386668|74.167012|33.154601| +---+---------+---------+---------+---------+ summary(y ~ I(x %in% c(1,2)) + f, data = d, fun=mean, method="cross", overall=TRUE) UseMethod by I(x %in% c(1, 2)), f +-+ |N| |y| +-+ +-----------------+---------+---------+---------+---------+ |I(x %in% c(1, 2))| lev1 | lev2 | lev3 | ALL | +-----------------+---------+---------+---------+---------+ | FALSE |12 |12 |12 |36 | | | 6.898417|19.591938|80.411102|35.633819| +-----------------+---------+---------+---------+---------+ | TRUE | 8 | 8 | 8 |24 | | | 6.927684|16.578763|64.800877|29.435774| +-----------------+---------+---------+---------+---------+ | ALL |20 |20 |20 |60 | | | 6.910124|18.386668|74.167012|33.154601| +-----------------+---------+---------+---------+---------+ > Regards, > Lauri > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 > and provide commented, minimal, self-contained, reproducible code. -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 ______________________________________________ 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 and provide commented, minimal, self-contained, reproducible code.