Rev 44243 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/stats/man/ftable.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2007 R Core Development Team% Distributed under GPL 2 or later\name{ftable}\title{Flat Contingency Tables}\alias{ftable}\alias{ftable.default}\alias{print.ftable}\description{Create \sQuote{flat} contingency tables.}\usage{ftable(x, \dots)\method{ftable}{default}(\dots, exclude = c(NA, NaN), row.vars = NULL,col.vars = NULL)}\arguments{\item{x, \dots}{\R objects which can be interpreted as factors (includingcharacter strings), or a list (or data frame) whose components canbe so interpreted, or a contingency table object of class\code{"table"} or \code{"ftable"}.}\item{exclude}{values to use in the exclude argument of \code{factor}when interpreting non-factor objects.}\item{row.vars}{a vector of integers giving the numbers of thevariables, or a character vector giving the names of the variablesto be used for the rows of the flat contingency table.}\item{col.vars}{a vector of integers giving the numbers of thevariables, or a character vector giving the names of the variablesto be used for the columns of the flat contingency table.}}\value{\code{ftable} returns an object of class \code{"ftable"}, which is amatrix with counts of each combination of the levels of variables withinformation on the names and levels of the (row and columns) variablesstored as attributes \code{"row.vars"} and \code{"col.vars"}.}\details{\code{ftable} creates \sQuote{flat} contingency tables. Similar to theusual contingency tables, these contain the counts of each combinationof the levels of the variables (factors) involved. This informationis then re-arranged as a matrix whose rows and columns correspond tounique combinations of the levels of the row and column variables (asspecified by \code{row.vars} and \code{col.vars}, respectively). Thecombinations are created by looping over the variables in reverseorder (so that the levels of the left-most variable vary theslowest). Displaying a contingency table in this flat matrix form(via \code{print.ftable}, the print method for objects of class\code{"ftable"}) is often preferable to showing it as ahigher-dimensional array.\code{ftable} is a generic function. Its default method,\code{ftable.default}, first creates a contingency table in arrayform from all arguments except \code{row.vars} and \code{col.vars}.If the first argument is of class \code{"table"}, it represents acontingency table and is used as is; if it is a flat table of class\code{"ftable"}, the information it contains is converted to the usualarray representation using \code{as.ftable}. Otherwise, the argumentsshould be \R objects which can be interpreted as factors (includingcharacter strings), or a list (or data frame) whose components can beso interpreted, which are cross-tabulated using \code{\link{table}}.Then, the arguments \code{row.vars} and \code{col.vars} are used tocollapse the contingency table into flat form. If neither of thesetwo is given, the last variable is used for the columns. If both aregiven and their union is a proper subset of all variables involved,the other variables are summed out.When the arguments are \R expressions interpreted as factors,additional arguments will be passed to \code{table} to control howthe variable names are displayed; see the last example below.Function \code{\link{ftable.formula}} provides a formula method forcreating flat contingency tables.There are methods for \code{\link{as.table}} and\code{\link{as.data.frame}}.}\seealso{\code{\link{ftable.formula}} for the formula interface (which allows a\code{data = .} argument);\code{\link{read.ftable}} for information on reading, writing andcoercing flat contingency tables;\code{\link{table}} for ordinary cross-tabulation;\code{\link{xtabs}} for formula-based cross-tabulation.}\examples{## Start with a contingency table.ftable(Titanic, row.vars = 1:3)ftable(Titanic, row.vars = 1:2, col.vars = "Survived")ftable(Titanic, row.vars = 2:1, col.vars = "Survived")## Start with a data frame.x <- ftable(mtcars[c("cyl", "vs", "am", "gear")])xftable(x, row.vars = c(2, 4))## Start with expressions, use table()'s "dnn" to change labelsftable(mtcars$cyl, mtcars$vs, mtcars$am, mtcars$gear, row.vars = c(2, 4),dnn = c("Cylinders", "V/S", "Transmission", "Gears"))}\keyword{category}