addmargins
Puts Arbitrary Margins on Multidimensional Tables or Arrays

Description

Add arbitrary margins on the multidimensional arrays or tables.

Usage

addmargins(A, margin = seq_along(dim(A)), FUN = sum, quiet = FALSE) 

Arguments

A a multidimensional array or table.
margin a vector specifying the index and order of dimensions to add margins.
FUN a function or a list of functions to be applied as margins. The length of FUN is either 1 or same as the length of margin. If it is a list, each element can also be a function or a list of functions.
quiet a logical value. If TRUE, the order of margins computed over dimensions will not be shown. This argument is effective only when FUN is specified and the number of margins to be added is more than one.

Details

addmargins applies each function that is specified in FUN on each array or table dimension that is defined in margin by order. The applied result is appended as a margin into the corresponding dimension of original array or table. The names of added margins are the names of FUN if it is specified, or "Sum" for the default FUN, or the substitute names of function if names are not specified. If a sub list is defined in the named list of FUN, the margins names are prefixed with the top names (if present), and separated with ".". See the Examples.
Value
returns an expanded array or a table like A with extra added margins and their names.
See Also
apply, table
Examples
set.seed(1)
x.array <- array(sample.int(10, 24, replace = TRUE), dim = c(4, 2, 3), 
	dimnames = list(Group = c( "A", "B", "C", "D"), Color = c("Red", "Blue"), 
	Year = c("1990", "2000", "2010")) )
addmargins(x.array)

# add margins to "Year" and "Group" dimensions with named function "sum" and "mean" addmargins(x.array, margin = c(3, 1), FUN =list(Total = sum, Average = mean) )

# add margins to "Year" and "Group" dimensions with unnamed function "mean", "max" and "min" addmargins(x.array, margin = c(3, 1), FUN =list(sum, list(max, min)) ) # almost same as above, but with the named list "Range" for unnamed "max" and unnamed "min" addmargins(x.array, margin = c(3, 1), FUN =list(sum, Range = list(max, min)) ) # almost same as above, but with the named list "Range" for named "max" and named "min" addmargins(x.array, margin = c(3, 1), FUN =list(sum, Range = list(Max = max, Min = min)) )

# Don't display the message of the order of margins computed over dimensions addmargins(x.array, margin = c(3, 1), FUN =list(Total = sum, Average = mean), quiet = TRUE )

# Example for a table. x <- sample( 1:7, 20, replace=TRUE) y <- sample( 1:7, 20, replace=TRUE) addmargins( table(x, y) )

Package stats version 6.0.0-69
Package Index