R: Expand contingency table into individual-level data set
expand.table
R Documentation
Expand contingency table into individual-level data set
Description
Expands contingency table or array into individual-level data set.
Usage
expand.table(x)
Arguments
x
table or array with dimnames(x) and
names(dimnames(x))
Details
For educational purposes, one may want to convert a multi-dimensional
contingency table into an individual-level data frame. In R,
multi-dimensional contigency tables are represented by arrays. An
array can be created using the array command, or the
table command with 3 or more vectors (usually fields from a data
frame).
It is this array, x, that is processed by
expand.table. In order to generate a data frame,
expand.table needs to process the field names and the possible
values for each field. The array x must have dimension names [i.e.,
dimnames(x)] and field names [i.e.,
names(dimnames(x))]. The expand.table function converts
names(dimnames(x)) to field names and the dimnames(x) to
factor levels for each field. Study the examples.
An ftable object, say ftab, can be expanded using
expand.table(as.table(ftab)).
Study the Titanic example to compare how a data frame can contain
either individual-level data or group-level data.