structable.RdThis function produces a ‘flat’ representation of a high-dimensional contingency table constructed by recursive splits (similar to the construction of mosaic displays).
# S3 method for class 'formula'
structable(formula, data,
direction = NULL, split_vertical = NULL, ..., subset, na.action)
# Default S3 method
structable(..., direction = NULL, split_vertical = FALSE)a formula object with possibly both left and right hand sides specifying the column and row variables of the flat table.
a data frame, list or environment containing the variables
to be cross-tabulated, or an object inheriting from class table.
an optional vector specifying a subset of observations
to be used.
Ignored if data is a contingency table.
a function which indicates what should happen when
the data contain NAs.
Ignored if data is a contingency table
R objects which can be interpreted as factors (including
character strings), or a list (or data frame) whose components can
be so interpreted, or a contingency table object of class
"table" or "ftable".
logical vector indicating, for each dimension,
whether it should be split vertically or not (default:
FALSE). Values are recycled as needed.
If the argument is of length 1, the value is alternated
for all dimensions. Ignored if direction is provided.
character vector alternatively specifying the
splitting direction ("h" for horizontal and "v" for
vertical splits). Values are recycled as needed. If the argument
is of length 1, the value is alternated for all dimensions.
This function produces textual representations of mosaic displays, and
thus ‘flat’ contingency tables. The formula interface is quite
similar to the one of ftable, but also accepts the
mosaic-like formula interface (empty left-hand
side). Note that even if the ftable interface is used,
the split_vertical or direction argument is needed to
specify the order of the horizontal and vertical splits.
If pretabulated data with a Freq column is used, than the
left-hand side should be left empty—the Freq column will be
handled correctly.
"structable" objects can be subset using the [ and [[
operators, using either level indices or names (see
examples). The corresponding replacement functions are available as well. In
addition, appropriate aperm, cbind,
rbind, length, dim, and
is.na methods do exist.
An object of class "structable",
inheriting from class "ftable", with the splitting
information ("split_vertical") as additional attribute.
Meyer, D., Zeileis, A., and Hornik, K. (2006),
The strucplot framework: Visualizing multi-way contingency tables with
vcd.
Journal of Statistical Software, 17(3), 1-48.
doi:10.18637/jss.v017.i03
and available as
vignette("strucplot").
structable(Titanic)
#> Sex Male Female
#> Survived No Yes No Yes
#> Class Age
#> 1st Child 0 5 0 1
#> Adult 118 57 4 140
#> 2nd Child 0 11 0 13
#> Adult 154 14 13 80
#> 3rd Child 35 13 17 14
#> Adult 387 75 89 76
#> Crew Child 0 0 0 0
#> Adult 670 192 3 20
structable(Titanic, split_vertical = c(TRUE, TRUE, FALSE, FALSE))
#> Class 1st 2nd 3rd Crew
#> Sex Male Female Male Female Male Female Male Female
#> Age Survived
#> Child No 0 0 0 0 35 17 0 0
#> Yes 5 1 11 13 13 14 0 0
#> Adult No 118 4 154 13 387 89 670 3
#> Yes 57 140 14 80 75 76 192 20
structable(Titanic, direction = c("h","h","v","v"))
#> Age Child Adult
#> Survived No Yes No Yes
#> Class Sex
#> 1st Male 0 5 118 57
#> Female 0 1 4 140
#> 2nd Male 0 11 154 14
#> Female 0 13 13 80
#> 3rd Male 35 13 387 75
#> Female 17 14 89 76
#> Crew Male 0 0 670 192
#> Female 0 0 3 20
structable(Sex + Class ~ Survived + Age, data = Titanic)
#> Sex Male Female
#> Class 1st 2nd 3rd Crew 1st 2nd 3rd Crew
#> Survived Age
#> No Child 0 0 35 0 0 0 17 0
#> Adult 118 154 387 670 4 13 89 3
#> Yes Child 5 11 13 0 1 13 14 0
#> Adult 57 14 75 192 140 80 76 20
## subsetting of structable objects
(hec <- structable(aperm(HairEyeColor)))
#> Eye Brown Blue Hazel Green
#> Sex Hair
#> Male Black 32 11 10 3
#> Brown 53 50 25 15
#> Red 10 10 7 7
#> Blond 3 30 5 8
#> Female Black 36 9 5 2
#> Brown 66 34 29 14
#> Red 16 7 7 7
#> Blond 4 64 5 8
## The "[" operator treats structables as a block-matrix and selects parts of the matrix:
hec[1]
#> Eye Brown Blue Hazel Green
#> Sex Hair
#> Male Black 32 11 10 3
#> Brown 53 50 25 15
#> Red 10 10 7 7
#> Blond 3 30 5 8
hec[2]
#> Eye Brown Blue Hazel Green
#> Sex Hair
#> Female Black 36 9 5 2
#> Brown 66 34 29 14
#> Red 16 7 7 7
#> Blond 4 64 5 8
hec[1,c(2,4)]
#> Eye Blue Green
#> Sex Hair
#> Male Black 11 3
#> Brown 50 15
#> Red 10 7
#> Blond 30 8
hec["Male",c("Blue","Green")]
#> Eye Blue Green
#> Sex Hair
#> Male Black 11 3
#> Brown 50 15
#> Red 10 7
#> Blond 30 8
## replacement funcion:
tmp <- hec
(tmp[1,2:3] <- tmp[2,c(1,4)])
#> Eye Brown Green
#> Sex Hair
#> Female Black 36 2
#> Brown 66 14
#> Red 16 7
#> Blond 4 8
## In contrast, the "[[" operator treats structables as two-dimensional
## lists. Indexing conditions on specified levels and thus reduces the dimensionality:
## seek subtables conditioning on levels of the first dimension:
hec[[1]]
#> Eye Brown Blue Hazel Green
#> Hair
#> Black 32 11 10 3
#> Brown 53 50 25 15
#> Red 10 10 7 7
#> Blond 3 30 5 8
hec[[2]]
#> Eye Brown Blue Hazel Green
#> Hair
#> Black 36 9 5 2
#> Brown 66 34 29 14
#> Red 16 7 7 7
#> Blond 4 64 5 8
## Seek subtable from the first two dimensions, given the level "Male"
## of the first variable, and "Brown" from the second
## (the following two commands are equivalent):
hec[["Male"]][["Brown"]]
#> Hair
#> Black 32
#> Brown 53
#> Red 10
#> Blond 3
hec[[c("Male","Brown")]]
#> Hair
#> Black 32
#> Brown 53
#> Red 10
#> Blond 3
## Seeking subtables by conditioning on row and/or column variables:
hec[["Male","Hazel"]]
#> Hair
#> Black 10
#> Brown 25
#> Red 7
#> Blond 5
hec[[c("Male","Brown"),]]
#> Eye Brown Blue Hazel Green
#>
#> 53 50 25 15
hec[[c("Male","Brown"),"Hazel"]]
#>
#> 25
## a few other operations
t(hec)
#> Sex Male Female
#> Hair Black Brown Red Blond Black Brown Red Blond
#> Eye
#> Brown 32 53 10 3 36 66 16 4
#> Blue 11 50 10 30 9 34 7 64
#> Hazel 10 25 7 5 5 29 7 5
#> Green 3 15 7 8 2 14 7 8
dim(hec)
#> [1] 2 4 4
dimnames(hec)
#> $Sex
#> [1] "Male" "Female"
#>
#> $Eye
#> [1] "Brown" "Blue" "Hazel" "Green"
#>
#> $Hair
#> [1] "Black" "Brown" "Red" "Blond"
#>
as.matrix(hec)
#> Eye
#> Sex_Hair Brown Blue Hazel Green
#> Male_Black 32 11 10 3
#> Male_Brown 53 50 25 15
#> Male_Red 10 10 7 7
#> Male_Blond 3 30 5 8
#> Female_Black 36 9 5 2
#> Female_Brown 66 34 29 14
#> Female_Red 16 7 7 7
#> Female_Blond 4 64 5 8
length(hec)
#> [1] 2
cbind(hec[,1],hec[,3])
#> Eye Brown Hazel
#> Sex Hair
#> Male Black 32 10
#> Brown 53 25
#> Red 10 7
#> Blond 3 5
#> Female Black 36 5
#> Brown 66 29
#> Red 16 7
#> Blond 4 5
as.vector(hec) ## computed on the _multiway_ table
#> [1] 32 36 11 9 10 5 3 2 53 66 50 34 25 29 15 14 10 16 10 7 7 7 7 7 3
#> [26] 4 30 64 5 5 8 8
as.vector(unclass(hec))
#> [1] 32 53 10 3 36 66 16 4 11 50 10 30 9 34 7 64 10 25 7 5 5 29 7 5 3
#> [26] 15 7 8 2 14 7 8