ftable
Flat Contingency Tables

Description

Create, reshape, and display "flat" contingency tables.

Usage

# generic function
ftable(x, ...)
## Default S3 method:
ftable(..., exclude = c(NA, NaN), row.vars = NULL, col.vars = NULL)
## S3 method for class 'formula':
ftable(formula, data = NULL, subset = NULL, na.action = getOption("na.action", default="na.fail"), ...)
## S3 method for class 'ftable':
print(x, digits = getOption("digits"), ...)
## S3 method for class 'ftable':
format(x, quote = TRUE, digits = getOption("digits"), ...)
## S3 method for class 'ftable':
as.table(x, ...)
## S3 method for class 'ftable':
as.data.frame(x, row.names = NULL, optional = FALSE, ...) 

Arguments

x In ftable, this argument could be a list, or a data frame, or a table object of class "table" or "ftable", or a formula object.
In other methods *.ftable, this argument is an object of class "ftable".
... In ftable and ftable.default, this argument could be one or more objects, each to be interpreted as a factor. All arguments must be of equal length. If this is a single argument (that is, a list, or a data frame, or a contingency table object of class "table" or "ftable".), each of its components is interpreted as a factor. Together, the arguments define a multi-way ragged array of as many dimensions as there are arguments.
In ftable.formula and other methods *.ftable, this argument is(are) other optional argument(s) passed to or from these functions.
exclude a vector of objects to exclude from forming levels when converting input vectors to factors before tabulating them. This argument is passed to function table and thus is only used if an argument to table is not a factor.
row.vars a character vector specifying the names or an integer vector specifying the numbers, of the variables to be used for the rows of the flat contingency table. If only one of row.vars and col.vars is supplied, the missing one is assumed to be all the names in the data argument except the names in the nonmissing argument.
col.vars a character vector specifying the names or an integer vector specifying the numbers, of the variables to be used for the columns of the flat contingency table.
formula a two-sided formula. The names to the left of the tilde are the col.vars and those the the right row.vars. The formula should contain only addition signs, names, and a tilde except that (a) a literal 1 on one side of the formula is used as a placeholder to indicate that there are no variables on that side of the formula and (b) a literal dot, ., is replaced by all the variables in the data that are not explicitly mentioned in the formula (only done when data is a ftable or table object)
data a data.frame, ftable, or table object that contains the objects named in formula.
subset an expression that specifies a subset from the data.frame data to use in formula. It is evaluated in the same environment as the formula is, so it can involve variables in the data.frame. A missing subset argument or subset=NULL means to use the entire data.frame. subset is ignored when data is a table or ftable object.
na.action a character string that specifies how missing values ("NA"s) are handled. By default, an error is returned. It is ignored when data is a table or ftable object.
digits the number of significant digits that should be printed or formatted. The default is the value of option "digits". This is only used when the numbers in the table are not integral.
quote a logical value. If set to TRUE(the default), the strings in the row and column name information are formatted with surrounding quotes. (The data in the table is self is never quoted.)
row.names a (character) vector giving row names for the data frame. Refer to as.data.frame for more information.
optional a logical value. Refer to as.data.frame for more information.

Details

A "flat table" (ftable object) is an alternate representation for a multiway table, as would be produced by table or tapply. Instead of a multiway array the data is stored in a two dimensional array and the table's dimnames with names are replaced by the the attributes row.vars and col.vars. The Cartesian product of the row.vars is used to label the rows of the ftable and the Cartesian product of the col.vars labels the columns. When printed, the row and column labels for any variable are only printed for the row or column in which there is a change in the value of the variable.
ftable is a generic function. Its default method ftable.default(invisible) creates a contingency tables with all arguments in ... firstly. This is done by calling table or as.table(for "ftable" object) normally. After that, this contingency table is permuted(via aperm) as a matrix(flat form) whose rows and columns correspond to unique combinations of the levels of the row and column variables . The row and column variables passed to aperm are reverse order. If either row.vars or col.vars is specified, the other one is the remaining of the all variables. If both of them are not specified, the last one variable is used for column and the remaining variables are used for row. The number of observations could be summed up for different combinations of levels of variables.
ftable.formula is an invisible formula method of the generic function ftable to create flat contingency table. The argument formula must have both left and right hand sides, which specify the column and row variables of flat contingency table, respectively. Only the "+" operator is allowed for combining the variables. A "." symbol used in the formula represents all the remaining variables. The data could be a data frame or matrix, or a table object of class "table" or "ftable". Finally, the ftable.default is called to create a flat contingency table.
print.ftable is a print method for object of class "ftable". It prints out a contingency table in flat matrix form.
format.ftable is an invisible format method for for object of class "ftable". It formats a flat contingency table to a character matrix form.
as.table.ftable is an invisible method of generic function as.table for object of class "ftable". It coerces an object of class "ftable" to a normal contingency table.
as.data.frame.ftable is an invisible method of generic function as.data.frame for object of class "ftable". It coerces an object of class "ftable" to a data frame.
Value
ftable and its methods returns an object of class "ftable", which is a matrix with counts of each combination of the levels of variables. It also contains two attributes "row.vars" and "col.vars" where the list of row and column variables are stored.
For the value of other methods *.ftable, please refer to the document for their generic function.
See Also
table, aperm as.data.frame, format, print
Examples
  Pet <- c("Cat","Dog","Cat","Dog","Cat","Fish")
  Food <- c("F1","F3","F2","F4","F2","F4")
  Sex <- c("M", "M", "F", "M", "F", "F")
  Color <- c("Black", "White", "Yellow", "NA", "White", "Black" )
  ft <- ftable(Pet, Food, Sex, Color)
  ft
  ft3 <- ftable(ft, row.vars = "Food", col.vars = c("Sex", "Pet"))
  ft3
  as.table(ft3)

M <- with(Sdatasets::solder, tapply(skips, list(Opening=Opening, Solder=Solder, PadType=PadType), FUN=mean)) print(ftable(M, row.vars=c("PadType", "Solder")), digits=2)

Admissions <- array(c(93, 56, 54, 98, 46, 96, 63, 72, 65, 33, 10, 41, 30, 50, 66, 61, 87, 84, 38, 36), dim=c(2, 2, 5), dimnames=list(Admit=c("Admitted", "Rejected"), Gender=c("Male", "Female"), Dept=LETTERS[1:5])) z <- ftable(Dept ~ ., data = Admissions) ftable(Admit ~ Dept + Gender, data = z) as.data.frame(z)

Package stats version 6.0.0-69
Package Index