Learn R Programming

vcdExtra (version 0.5-2)

collapse.table: Collapse levels of a table

Description

Collapse (or re-label) variables in a a contingency table or ftable by re-assigning levels of the table variables

Usage

collapse.table(table, ...)

Arguments

table
A table or ftable object
...
A collection of one or more assignments of factors of the table to a list of levels

Value

  • A xtabs and table objects, representing the original table with one or more of its factors collapsed or rearranged into other levels.

Details

Each of the ... arguments must be of the form variable = levels, where variable is the name of one of the table dimensions, and levels is a character or numeric vector of length equal to the corresponding dimension of the table.

See Also

expand.dft margin.table "collapses" a table in a different way, by summing over table dimensions.

Examples

Run this code
# create some sample data in table form
sex <- c("Male", "Female")
age <- letters[1:6]
education <- c("low", 'med', 'high')
data <- expand.grid(sex=sex, age=age, education=education)
counts <- rpois(36, 100) 
data <- cbind(data, counts)
t1 <- xtabs(counts ~ sex + age + education, data=data)

# collapse age to 3 levels
t2 <- collapse.table(t1, age=c("A", "A", "B", "B", "C", "C"))

# collapse age to 3 levels and pool education: "low" and "med" to "low"
t3 <- collapse.table(t1, age=c("A", "A", "B", "B", "C", "C"), 
    education=c("low", "low", "high"))

# change labels for levels of education to 1:3
t4 <- collapse.table(t1,  education=1:3)

Run the code above in your browser using DataLab