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)
... |
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.
|
useNA |
controls if an NA is a level in the resulting table.
|
dnn |
the names of the table dimnames attribute.
These names identify the dimensions of the resulting contingency table.
|
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:
|
x | any object of class "table" or any object inheriting from class "table". |
table | returns 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
|
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". |
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