xtabs
Cross Tabulation

Description

Create a multiway contingency table (a cross-tabulation) from a collection of factors.

Usage

xtabs(formula = ~., data = parent.frame(), subset, sparse = FALSE, 
    na.action, addNA = FALSE, exclude = c(NA, NaN),
    drop.unused.levels = FALSE) 

Arguments

formula a formula object (or an object which can be coerced to a formula) with the terms, separated by + operators, on the right of the ~. Each term on the right hand side should be a factor, and will be converted to one if not. If there is a term to the left of the ~ it should be a vector or matrix of counts -- this useful for data that has already been tabulated. If the formula is omitted or is default (~ .) and the data argument is a data frame, then all the variables in data will be cross-tabulated. Formula interactions are not allowed.
data a data frame or frame number telling where the variables named in the formula (and in the subset argument) may be found. If a variable is not found by searching in the data frame or frame given by data, it is expected to be on the search list.
subset expression telling which subset of the rows of the data should be used in the table. It can be an expression that evaluates to a logical vector, or a vector of logical values, or a vector of row numbers or row names---in short, anything you would normally use to subscript the rows of a data frame. The variable names in the expression should be names in the same place supplied by the data argument, otherwise they will be looked for on the search list. All observations are included by default.
sparse An unused argument here for compatibility with R.
na.action a function for handling missing values. If there are any missing values in the data to be cross-tabulated, the data will be put into a data frame and passed to the function given by na.action. See na.action for more details.
addNA a logical value. If FALSE (the default), rows of data containing missing values are omitted (unless you specify na.action=na.pass). If TRUE, all rows of data are used and the output table contains an <NA> category for each column that that contains missing values.
exclude a vector of values to be excluded when converting the factors from non-factor variables on the right hand side.
drop.unused.levels logical value. If TRUE then any unused levels in factors will be omitted from the table. If FALSE(the default) , they will not be dropped and the table will contain rows or columns of zeros for those unused levels. This will cause the marginal proportions for those levels and the overall chi-squared statistic to be NA's, but may be useful for making parallel tables of similar data sets.

Details

This function provides a formula interface to the table and tapply functions, for tabulation (counting the number of observations that fall in each cell in a contingency table). If you want to do other calculations, say, say compute means or sums for observations in cells, try tapply.
Value
An array of of class c("xtabs", "table"), with a "call" attribute containing the matched call.
See Also
factor, table, tapply, ftable.
Examples
xtabs(~Solder+Opening, data=Sdatasets::solder, subset=skips>10) 
#         Opening
# Solder   S  M  L
#  Thin  99 15  9
#  Thick 24 11  0
xtabs(skips~Mask, data=Sdatasets::solder)

ftable(xtabs(skips ~ Mask + Solder + PadType, data = Sdatasets::solder, subset = Panel=="2") )

summary(xtabs(skips ~ Mask + PadType, data = Sdatasets::solder))

petfood <- data.frame(Pet=c("Dog","Dog","Cat","Cat","Cat", "Dog", "Cat"), Food=c("Wet","Wet","Dry","Wet",NA, "Dry", NA)) xtabs(data=petfood, na.action=na.exclude) xtabs(data=petfood, addNA = TRUE) try(xtabs(data=petfood, na.action=na.fail)) # error due to missing values

Package stats version 6.1.4-13
Package Index