Rev 49086 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/stats/man/addmargins.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2009 R Core Development Team% Distributed under GPL 2 or later\name{addmargins}\alias{addmargins}\concept{totals}\concept{margins}\title{Puts Arbitrary Margins on Multidimensional Tables or Arrays}\description{For a given table one can specify which of the classifying factors toexpand by one or more levels to hold margins to be calculated. One may forexample form sums and means over the first dimension and medians over thesecond. The resulting table will then have two extra levels for the firstdimension and one extra level for the second. The default is to sum overall margins in the table. Other possibilities may give results thatdepend on the order in which the margins are computed. This is flaggedin the printed output from the function.}\usage{addmargins(A, margin = seq_along(dim(A)), FUN = sum, quiet = FALSE)}\arguments{\item{A}{table or array. The function uses the presence of the\code{"dim"} and \code{"dimnames"} attributes of \code{A}.}\item{margin}{vector of dimensions over which to form margins. Marginsare formed in the order in which dimensions are specified in\code{margin}.}\item{FUN}{list of the same length as \code{margin}, each element ofthe list being either a function or a list of functions. Names ofthe list elements will appear as levels in dimnames of the result.Unnamed list elements will have names constructed: the nameof a function or a constructed name based on the position in the table.}\item{quiet}{logical which suppresses the message telling the order inwhich the margins were computed.}}\details{If the functions used to form margins are not commutative the resultdepends on the order in which margins are computed. Annotationof margins is done via naming the \code{FUN} list.}\value{A table or array with the same number of dimensions as \code{A}, butwith extra levels of the dimensions mentioned in \code{margin}. Thenumber of levels added to each dimension is the length of the entriesin \code{FUN}. A message with the order of computation of margins isprinted.}\author{Bendix Carstensen, Steno Diabetes Center & Department ofBiostatistics, University of Copenhagen,\url{http://www.biostat.ku.dk/~bxc}, autumn 2003.Margin naming enhanced by Duncan Murdoch.}\seealso{\code{\link{table}}, \code{\link{ftable}}, \code{\link{margin.table}}.}\examples{Aye <- sample(c("Yes", "Si", "Oui"), 177, replace = TRUE)Bee <- sample(c("Hum", "Buzz"), 177, replace = TRUE)Sea <- sample(c("White", "Black", "Red", "Dead"), 177, replace = TRUE)(A <- table(Aye, Bee, Sea))addmargins(A)\dontshow{stopifnot(is.table(addmargins(A)))}ftable(A)ftable(addmargins(A))# Non-commutative functions - note differences between resulting tables:ftable(addmargins(A, c(1,3),FUN = list(Sum = sum, list(Min = min, Max = max))))ftable(addmargins(A, c(3,1),FUN = list(list(Min = min, Max = max), Sum = sum)))# Weird function needed to return the N when computing percentagessqsm <- function(x) sum(x)^2/100B <- table(Sea, Bee)round(sweep(addmargins(B, 1, list(list(All = sum, N = sqsm))), 2,apply(B, 2, sum)/100, "/"), 1)round(sweep(addmargins(B, 2, list(list(All = sum, N = sqsm))), 1,apply(B, 1, sum)/100, "/"), 1)# A total over Bee requires formation of the Bee-margin first:mB <- addmargins(B, 2, FUN = list(list(Total = sum)))round(ftable(sweep(addmargins(mB, 1, list(list(All = sum, N = sqsm))), 2,apply(mB,2,sum)/100, "/")), 1)## Zero.Printing table+margins:set.seed(1)x <- sample( 1:7, 20, replace=TRUE)y <- sample( 1:7, 20, replace=TRUE)tx <- addmargins( table(x, y) )print(tx, zero.print = ".")}\keyword{manip}\keyword{array}