Last chance! 50% off unlimited learning
Sale ends in
This is a fully compatible extension of the standard xtabs()
function with the added option
to produce a sparse matrix result via sparse = TRUE
.
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 smodel.frame
) containing the variables in the
formula formula
. By default the variables are taken from
environment(formul
NA
s.FALSE
and
there are unused levels, the table will contain zero marginals, and
a subsequent chi-squared test for independence of the factorssparse=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
xtabs
results,
there is a summary
method for contingency table objects created
by table
or xtabs
, 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.
xtabs
and its
references.## See for non-sparse examples:
example(xtabs, package = "stats")
## similar to "nlme"s 'ergoStool' :
d.ergo <- data.frame(Type = paste("T", rep(1:4, 9*4), sep=""),
Subj = gl(9,4, 36*4))
xtabs(~ Type + Subj, data=d.ergo) # 4 replicates each
set.seed(15) # a subset of cases:
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)])
xtabs(~ inner + outer, fr, sparse = TRUE)
Run the code above in your browser using DataLab