Learn R Programming

SciencesPo (version 1.02.12)

margins: Puts Arbitrary Margins on Two-way Tables or Arrays

Description

Used to put marginal values on the two-way table or array. For tables or arrays with more dimensions see addmargins(). This version is designed for use in introductory classes where the margin= argument has been modified to be more intuitive to students. Specifically, This function is a pass-through to addmargins() with the exception that margin=1 adds a marginal value at the end of the rows and margin=2 adds a marginal value at the end of the columns. The numeric values in margin= are opposite of what is used in addmargins().

Usage

margins(table, margin = seq_along(dim(table)), ...)

Arguments

table
an array or a table class object.
margin
A numeric value over which to form margins. See details.
...
Additional arguments to be sent to addmargins. See ?addmargins.

Value

  • A table or array with the same number of dimensions as A, but with extra levels of the dimensions mentioned in margin.

See Also

and percTable in NCStats.

Examples

Run this code
Aye <- sample(c("Yes", "Si", "Oui"), 177, replace = TRUE)
( A1 <- table(Aye) )
Bee <- sample(c("Hum", "Buzz"), 177, replace = TRUE)
( A2 <- table(Aye, Bee) )

## 1-D table
# Add sums to margin
margins(A1)
# Add sums to margin of proportions table
margins(prop.table(A1))

## 2-D table
# Add sums to all margins
margins(A2)

# Add sums to end of rows (row totals)
margins(A2,margin=1)

# Add sums to end of columns (column totals)
margins(A2,margin=2)

# Add sums to end of a row proportions table
A2.row <- prop.table(A2,margin=1)
margins(A2.row, margin=1)

# Add sums to end of a column proportions table
A2.col <- prop.table(A2,margin=2)
margins(A2.col,margin=2)
# Add sums to all margins of proportions table
A2.tbl <- prop.table(A2)
margins(A2.tbl)

Run the code above in your browser using DataLab