table
Create a Contingency Table from Factors

Description

Returns a contingency table (an array) with the same number of dimensions as the given arguments.

Usage

table(..., exclude = if (useNA == "no") c(NA, NaN),
      useNA = c("no", "ifany", "always"),
      dnn = list.names(...), deparse.level = 1) 
as.table(x, ...)
is.table(x)
## S3 method for class 'table':
[(x, i, j, ..., drop = TRUE)

Arguments

... For table, one or more objects, each to be interpreted as a factor. All arguments must be of equal length. Missing values (NAs) are allowed. If this is a single argument (that is, a list or a data.frame), 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. Arguments that are given in the form name=value have that name in the dimnames list.

For [.table, i, j, and all arguments in ... are used as subscripts to x.

exclude a vector of objects to exclude from forming levels.
  • If useNA is "no", the default value excludes NA values.
  • If useNA is specified as NULL, the argument useNA is treated as "always".
useNA controls if an NA is a level in the resulting table.
  • If "no", ignores NA values in the table inputs.
  • If "ifany", includes NA values in the table.
  • If "always", adds an extra NA value added to each factor level if the NA does not already exist.
dnn the names of the table dimnames attribute. These names identify the dimensions of the resulting contingency table.
  • If you do not specify a dnn argument, the internal function list.names is called to generate the names from the argument names (if they are given).
  • If you do not specify an input object with an argument name, you can deparse the argument expression to produce a suitable name, according to the argument deparse.level.
deparse.level controls how the names of the table dimnames attribute are generated if the dnn argument is not specified.

If an input argument is specified with an argument name, that argument name is used for that dimension. Otherwise, the name used is controlled by deparse.level, as follows:

  • If deparse.level = 0, the name is the empty string.
  • If deparse.level = 1, and if the variable name is specified as a single variable, the name is the variable name of the corresponding argument. Otherwise it is the empty string.
  • If deparse.level = 2, the name is the deparsed value of the argument expression.
x any object of class "table" or any object inheriting from class "table".

Details

table returns a multi-way array containing the number of observations in the cells defined by the arguments to table. as.table is generic. It implements the method as.table.default.
Value

tablereturns an object of class "table", representing a multi-way array containing the number of observations in the cells defined by the arguments to table. It has dim and dimnames attributes like a matrix or an array, so its elements can be extracted with the normal bracket operations. The table's dimnames attribute contains the levels attribute of each of the arguments.

For example, if a table is created with

    z <- table(arg1, arg2, arg3)
then z[i,j,k] is the number of times that the combination of the i-th level of the first factor, the j-th level of the second, and the k-th level of the third appeared together in the data. A combination is also counted if missing values are present in the corresponding element of any of the arguments (unless useNA is "no", or the missing value is excluded with the exclude argument).
as.table coerces its x argument to a table.
is.table returns TRUE if its x argument has the class "table" or is inherited from class "table".
[.table subscripts the table in the same way that arrays are subscripted, but also copies the class of its x argument to its output if the output is also an array.
See Also
print.table, cut, factor, tapply, tabulate
Examples
Pet <- factor(c("Cat","Dog","Cat","Dog","Cat","?"), exclude="?")
Food <- factor(c("Dry","Dry","Dry","Wet","Wet","Wet"))

# make table: get dimnames from argument names table(X=Pet, Y=Food) # Y # X Dry Wet # Cat 2 1 # Dog 1 1

# make table: get dimnames from "dnn" argument table(Pet[1:4], Food[1:4], dnn=c("A","B")) # B # A Dry Wet # Cat 2 0 # Dog 1 1

# make table: get dimnames from variable names table(Pet, Food) # Food # Pet Dry Wet # Cat 2 1 # Dog 1 1

# make table: get dimnames from deparsed expressions # (unless explicitly specified as argument names) table(X=Pet[1:4], Food[1:4], deparse.level=2) # Food[1:4] # X Dry Wet # Cat 2 0 # Dog 1 1

# make table from single list or data.frame table(list(X=Pet, Y=Food)) # Y # X Dry Wet # Cat 2 1 # Dog 1 1 table(data.frame(Pet=Pet, Food=Food)) # Food # Pet Dry Wet # Cat 2 1 # Dog 1 1

# useNA = "no": by default, don't tabulate NA values table(Pet, Food, useNA = "no") # Food # Pet Dry Wet # Cat 2 1 # Dog 1 1

# useNA = "ifany": tabulate NA value, if present table(Pet, Food, useNA = "ifany") # Food # Pet Dry Wet # Cat 2 1 # Dog 1 1 # <NA> 0 1

# useNA = "always": tabulate NA value, even if not present table(Pet, Food, useNA = "always") # Food # Pet Dry Wet <NA> # Cat 2 1 0 # Dog 1 1 0 # <NA> 0 1 0

# exclude = NULL: same as useNA = "always" table(Pet, Food, exclude = NULL) # Food # Pet Dry Wet <NA> # Cat 2 1 0 # Dog 1 1 0 # <NA> 0 1 0

# Extract part of table corresponding to Food=="Wet" table(Pet, Food, useNA = "ifany")[, "Wet", drop=FALSE] # Food # Pet Wet # Cat 1 # Dog 1 # <NA> 1

x <- cbind(as.vector(Pet), as.vector(Food)) is.table(x) # [1] FALSE x # [,1] [,2] # [1,] "Cat" "Dry" # [2,] "Dog" "Dry" # [3,] "Cat" "Dry" # [4,] "Dog" "Wet" # [5,] "Cat" "Wet" # [6,] NA "Wet"

y <- as.table(x) is.table(y) # [1] TRUE y # A B # A Cat Dry # B Dog Dry # C Cat Dry # D Dog Wet # E Cat Wet # F Wet

Package base version 6.1.1-7
Package Index