[R] tapply grand mean

Chuck Cleland ccleland at optonline.net
Wed Aug 8 15:03:39 CEST 2007


Lauri Nikkinen wrote:
> Thanks Chuck but I would fancy the output made by tapply because the
> idea is to make a barplot based on those values.
>  
> -Lauri

sum1 <- summary(y ~ x + f, data = d, fun=mean,
                method="cross", overall=TRUE)

df <- data.frame(x = sum1$x, f = sum1$f, y = sum1$S)

df
     x    f         y
1    1 lev1  6.452326
2    2 lev1  7.403041
3    3 lev1  6.117648
4    4 lev1  7.831390
5    5 lev1  6.746213
6  ALL lev1  6.910124
7    1 lev2 15.861256
8    2 lev2 17.296270
9    3 lev2 17.976864
10   4 lev2 19.696998
11   5 lev2 21.101952
12 ALL lev2 18.386668
13   1 lev3 61.393455
14   2 lev3 68.208299
15   3 lev3 73.479837
16   4 lev3 80.323382
17   5 lev3 87.430087
18 ALL lev3 74.167012
19   1  ALL 27.902346
20   2  ALL 30.969203
21   3  ALL 32.524783
22   4  ALL 35.950590
23   5  ALL 38.426084
24 ALL  ALL 33.154601

library(lattice)

barchart(y ~ x | f, data = df, layout=c(4,1,1))

OR

barchart(S ~ x | f, data = sum1, layout=c(4,1,1))

> 2007/8/8, Chuck Cleland <ccleland at optonline.net
> <mailto:ccleland at optonline.net>>:
> 
>     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 at stat.math.ethz.ch <mailto:R-help at 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 

-- 
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



More information about the R-help mailing list