Description Usage Arguments Details Value Author(s) References See Also Examples
Expands contingency table or array into individual-level data set.
1 | expand.table(x)
|
x |
table or array with |
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.
Returns an individual-level data frame
Tomas Aragon, aragon@berkeley.edu, http://www.phdata.science; Daniel Wollschlaeger, dwoll@psychologie.uni-kiel.de, http://www.uni-kiel.de/psychologie/dwoll/
none
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.