margin.table
Compute Table Margin

Description

Computes the sum of values in a table over the specified margins.

Usage

marginSums(x, margin = NULL)
margin.table(x, margin = NULL)

Arguments

x a numeric matrix, table, or array.
margin a vector of integers or character strings defining which margins over which to sum. For example, if x is a matrix, margin=1 indicates rows and margin=2 indicates columns. If character strings, they are expected to be a subset of names(dimnames(x)).

Note that margin tells which dimensions of x are retained in the result.

If margin is NULL, sum of x is returned.

Details

This function returns a vector or an array that is the result of applying the sum function on x with the specified margin if margin is not NULL, or the sum of x if margin is NULL.
The dimension size and dimension names of the computed array are extracted as dim(x)[margin] and dimnames(x)[margin], respectively.
The returned vector or array has the same class as x.
Value
returns a numeric vector or array that is the result of applying the sum function on x with the specified margin if margin is not NULL, or sum of x if margin is NULL.
Note
Version 4.0.0 of open-source R introduced a new name, marginSums, for the old margin.table function, in the hopes that its purpose would be more obvious. The functions are identical.
See Also
apply, table, prop.table.
Examples
x <- array(1:24, dim = c(4, 3, 2),
    dimnames=list(A=NULL, B=paste0("b",1:3), C=NULL))
marginSums(x, c(2, 3))
marginSums(x, c("B", "C"))
marginSums(x, 1)
margin.table(x)
Package base version 6.1.1-7
Package Index