expand.table: Expand contingency table into individual-level data set

Description Usage Arguments Details Value Author(s) References See Also Examples

Description

Expands contingency table or array into individual-level data set.

Usage

1

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.

Value

Returns an individual-level data frame

Author(s)

Tomas Aragon, aragon@berkeley.edu, http://www.phdata.science; Daniel Wollschlaeger, dwoll@psychologie.uni-kiel.de, http://www.uni-kiel.de/psychologie/dwoll/

References

none

See Also

expand.grid

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
##Creating array using 'array' function and expanding it
tab <- array(1:8, c(2, 2, 2))
dimnames(tab) <- list(c("No","Yes"), c("No","Yes"), c("No","Yes"))
names(dimnames(tab)) <- c("Exposure", "Disease", "Confounder")
tab
df <- expand.table(tab)
df

##Creating array using 'table' function and expanding it
tab2 <- table(Exposure = df$Exp, Disease = df$Dis, Confounder = df$Conf)
expand.table(tab2)

##Expanding ftable object
ftab2 <- ftable(tab2)
ftab2
expand.table(as.table(ftab2))

##Convert Titanic data into individual-level data frame
data(Titanic)
expand.table(Titanic)[1:20,]

##Convert Titanic data into group-level data frame
as.data.frame(Titanic)

epitools documentation built on March 26, 2020, 9:14 p.m.