margin.table
Compute Table Margin
Description
Computes the sum of values in a table over the specified margins.
Usage
margin.table(x, margin = NULL)
Arguments
x |
a numeric matrix, table, or array.
|
margin |
a vector of subscripts defining which margins over which to sum.
For example, if x is a matrix, margin=1 indicates rows
and margin=2 indicates columns.
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.
See Also
Examples
x <- array(1:120, dim = c(3, 2, 4, 5))
margin.table(x, c(2, 3))
# [,1] [,2] [,3] [,4]
# [1,] 750 840 930 1020
# [2,] 795 885 975 1065
margin.table(x, 1)
# [1] 2380 2420 2460
margin.table(x)
# [1] 7260