Create a contingency table as an array, similar to xtabs. However, whereas xtabs converts NA-s in 0, xtabs2 does not touch NA values. Moreover, while output class in xtabs is c("xtabs", "table"), it is an array in xtabs2.

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

Arguments

formula

a formula object with the cross-classifying variables (separated by +) on the right hand side (or an object which can be coerced to a formula). Interactions are not allowed. On the left hand side, one may optionally give a vector or a matrix of counts; in the latter case, the columns are interpreted as corresponding to the levels of a variable. This is useful if the data have already been tabulated, see the examples below.

data

an optional matrix or data frame (or similar: see model.frame) containing the variables in the formula formula. By default the variables are taken from environment(formula).

subset

an optional vector specifying a subset of observations to be used.

sparse

logical specifying if the result should be a sparse matrix, i.e., inheriting from sparseMatrix Only works for two factors (since there are no higher-order sparse array classes yet).

na.action

a function which indicates what should happen when the data contain NAs. If unspecified, and addNA is true, this is set to na.pass. When it is na.pass and formula has a left hand side (with counts), sum(*, na.rm = TRUE) is used instead of sum(*) for the counts.

exclude

a vector of values to be excluded when forming the set of levels of the classifying factors.

drop.unused.levels

a logical indicating whether to drop unused levels in the classifying factors. If this is FALSE and there are unused levels, the table will contain zero marginals, and a subsequent chi-squared test for independence of the factors will not work.

Value

An array.