xtabs(formula = ~., data = parent.frame(), subset, sparse = FALSE,
na.action, exclude = c(NA, NaN), drop.unused.levels = FALSE)
+
) 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.model.frame
) containing the variables in the
formula formula
. By default the variables are taken from
environment(formula)
.sparseMatrix
Only works for two factors (since there
are no higher-order sparse array classes yet).
NA
s.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.sparse = FALSE
,
a contingency table in array representation of S3 class c("xtabs",
"table")
, with a "call"
attribute storing the matched call. When sparse = TRUE
, a sparse numeric matrix, specifically an
object of S4 class
dgTMatrix
from package
https://CRAN.R-project.org/package=Matrix.summary
method for contingency table objects created
by table
or xtabs(*, sparse = FALSE)
, which gives basic
information and performs a chi-squared test for independence of
factors (note that the function chisq.test
currently
only handles 2-d tables). If a left hand side is given in formula
, its entries are simply
summed over the cells corresponding to the right hand side; this also
works if the lhs does not give counts. For variables in formula
which are factors, exclude
must be specified explicitly; the default exclusions will not be used.table
for traditional cross-tabulation, and
as.data.frame.table
which is the inverse operation of
xtabs
(see the DF
example below). sparseMatrix
on sparse
matrices in package https://CRAN.R-project.org/package=Matrix.## 'esoph' has the frequencies of cases and controls for all levels of
## the variables 'agegp', 'alcgp', and 'tobgp'.
xtabs(cbind(ncases, ncontrols) ~ ., data = esoph)
## Output is not really helpful ... flat tables are better:
ftable(xtabs(cbind(ncases, ncontrols) ~ ., data = esoph))
## In particular if we have fewer factors ...
ftable(xtabs(cbind(ncases, ncontrols) ~ agegp, data = esoph))
## This is already a contingency table in array form.
DF <- as.data.frame(UCBAdmissions)
## Now 'DF' is a data frame with a grid of the factors and the counts
## in variable 'Freq'.
DF
## Nice for taking margins ...
xtabs(Freq ~ Gender + Admit, DF)
## And for testing independence ...
summary(xtabs(Freq ~ ., DF))
## Create a nice display for the warp break data.
warpbreaks$replicate <- rep(1:9, len = 54)
ftable(xtabs(breaks ~ wool + tension + replicate, data = warpbreaks))
### ---- Sparse Examples ----
if(require("Matrix")) {
## similar to "nlme"s 'ergoStool' :
d.ergo <- data.frame(Type = paste0("T", rep(1:4, 9*4)),
Subj = gl(9, 4, 36*4))
print(xtabs(~ Type + Subj, data = d.ergo)) # 4 replicates each
set.seed(15) # a subset of cases:
print(xtabs(~ Type + Subj, data = d.ergo[sample(36, 10), ], sparse = TRUE))
## Hypothetical two-level setup:
inner <- factor(sample(letters[1:25], 100, replace = TRUE))
inout <- factor(sample(LETTERS[1:5], 25, replace = TRUE))
fr <- data.frame(inner = inner, outer = inout[as.integer(inner)])
print(xtabs(~ inner + outer, fr, sparse = TRUE))
}<!-- % only if Matrix is available -->
Run the code above in your browser using DataLab