Suppose I have data like this: A <- sample(letters[1:3], 1000, replace=TRUE) B <- sample(LETTERS[1:2], 1000, replace=TRUE) x <- rnorm(1000) I can get a table of means via tapply(x, list(A, B), mean) and I can add the marginal means to this using cbind/rbind: main <- tapply(x, list(A,B), mean) Amargin <- tapply(x, list(A), mean) Bmargin <- tapply(x, list(B), mean) rbind(cbind(main, all=Amargin),all=c(Bmargin, mean(x))) But this is tedious. Has some package got some code that makes this easier? Duncan Murdoch
I had to set it up as a data frame and then it workd beautifully with the reshape package. DF<-data.frame(A,B,x) library(reshape) cast(DF, A ~ B, fun.aggregate=mean, margins=c("grand_row", "grand_col")) Cheers Tyler ----------------------------------------> Date: Sun, 23 Oct 2011 14:39:08 -0400 > From: murdoch.duncan at gmail.com > To: R-help at r-project.org > Subject: [R] Summary stats in table > > Suppose I have data like this: > > A <- sample(letters[1:3], 1000, replace=TRUE) > B <- sample(LETTERS[1:2], 1000, replace=TRUE) > x <- rnorm(1000) > > I can get a table of means via > > tapply(x, list(A, B), mean) > > and I can add the marginal means to this using cbind/rbind: > > main <- tapply(x, list(A,B), mean) > Amargin <- tapply(x, list(A), mean) > Bmargin <- tapply(x, list(B), mean) > > rbind(cbind(main, all=Amargin),all=c(Bmargin, mean(x))) > > But this is tedious. Has some package got some code that makes this easier? > > Duncan Murdoch > > ______________________________________________ > R-help at r-project.org 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.
On Mon, Oct 24, 2011 at 5:39 AM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:> Suppose I have data like this: > > A <- sample(letters[1:3], 1000, replace=TRUE) > B <- sample(LETTERS[1:2], 1000, replace=TRUE) > x <- rnorm(1000) > > I can get a table of means via > > tapply(x, list(A, B), mean) > > and I can add the marginal means to this using cbind/rbind: > > main <- tapply(x, list(A,B), mean) > Amargin <- tapply(x, list(A), mean) > Bmargin <- tapply(x, list(B), mean) > > rbind(cbind(main, all=Amargin),all=c(Bmargin, mean(x))) > > But this is tedious. ?Has some package got some code that makes this easier?Have a look at reshape2::add_margins - it's not super efficient, but I think cool because it works for arbitrarily many dimensions. Hadley -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/