marginSums {base} | R Documentation |
For a contingency table in array form, compute the sum of table entries for a given margin or set of margins.
marginSums(x, margin = NULL)
margin.table(x, margin = NULL)
x |
an array, usually a |
margin |
a vector giving the margins to compute sums for.
E.g., for a matrix |
The relevant marginal table, or just the sum of all entries if margin
has length zero. The class of x
is copied to the
output table if margin
is non-NULL.
margin.table
is an earlier name, retained for back-compatibility.
Peter Dalgaard
rowSums
and colSums
for similar functionality.
proportions
and addmargins
.
m <- matrix(1:4, 2)
marginSums(m, 1) # = rowSums(m)
marginSums(m, 2) # = colSums(m)
DF <- as.data.frame(UCBAdmissions)
tbl <- xtabs(Freq ~ Gender + Admit, DF)
tbl
marginSums(tbl, "Gender") # a 1-dim "table"
rowSums(tbl) # a numeric vector