xtabs: Cross Tabulation (2024)

xtabsR Documentation

Description

Create a contingency table (optionally a sparse matrix) fromcross-classifying factors, usually contained in a data frame,using a formula interface.

Usage

xtabs(formula = ~., data = parent.frame(), subset, sparse = FALSE, na.action, addNA = FALSE, exclude = if(!addNA) c(NA, NaN), drop.unused.levels = FALSE)## S3 method for class 'xtabs'print(x, na.print = "", ...)

Arguments

formula

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

data

an optional matrix or data frame (or similar: seemodel.frame) containing the variables in theformula formula. By default the variables are taken fromenvironment(formula).

subset

an optional vector specifying a subset of observationsto be used.

sparse

logical specifying if the result should be asparse matrix, i.e., inheriting fromsparseMatrixOnly works for two factors (since thereare no higher-order sparse array classes yet).

na.action

a function which indicates what should happen whenthe data contain NAs. If unspecified, andaddNA is true, this is set to na.pass. When itis na.omit and formula has a left hand side (withcounts), sum(*, na.rm = TRUE) is used instead ofsum(*) for the counts.

addNA

logical indicating if NAs should get a separatelevel and be counted, using addNA(*, ifany=TRUE) andsetting the default for na.action to na.pass.

exclude

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

drop.unused.levels

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

x

an object of class "xtabs".

na.print

character string (or NULL) indicating howNA are printed. The default ("") does not showNAs clearly, and na.print = "NA" maybe advisableinstead.

...

further arguments passed to or from other methods.

Details

There is a summary method for contingency table objects createdby table or xtabs(*, sparse = FALSE), which gives basicinformation and performs a chi-squared test for independence offactors (note that the function chisq.test currentlyonly handles 2-d tables).

If a left hand side is given in formula, its entries are simplysummed over the cells corresponding to the right hand side; this alsoworks if the lhs does not give counts.

For variables in formula which are factors, excludemust be specified explicitly; the default exclusions will not be used.

In R versions before 3.4.0, e.g., when na.action = na.pass,sometimes zeroes (0) were returned instead of NAs.

Note that when addNA is false as by default, and na.actionis not specified (or set to NULL), in effect na.action = getOption("na.action", default=na.omit) is used; see also the examples.

Value

By default, when 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 anobject of S4 class dgTMatrix from packageMatrix.

See Also

table for traditional cross-tabulation, andas.data.frame.table which is the inverse operation ofxtabs (see the DF example below).

sparseMatrix on sparsematrices in package Matrix.

Examples

## '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))## with NA'sDN <- DF; DN[cbind(6:9, c(1:2,4,1))] <- NADN # 'Freq' is missing only for (Rejected, Female, B)tools::assertError(# 'na.fail' should fail : xtabs(Freq ~ Gender + Admit, DN, na.action=na.fail), verbose=TRUE)op <- options(na.action = "na.omit") # the "factory" default(xtabs(Freq ~ Gender + Admit, DN) -> xtD)noC <- function(O) `attr<-`(O, "call", NULL)ident_noC <- function(x,y) identical(noC(x), noC(y))stopifnot(exprs = { ident_noC(xtD, xtabs(Freq ~ Gender + Admit, DN, na.action = na.omit)) ident_noC(xtD, xtabs(Freq ~ Gender + Admit, DN, na.action = NULL))})xtabs(Freq ~ Gender + Admit, DN, na.action = na.pass)## The Female:Rejected combination has NA 'Freq' (and NA prints 'invisibly' as "")(xtNA <- xtabs(Freq ~ Gender + Admit, DN, addNA = TRUE)) # ==> count NAs## show NA's better via na.print = ".." :print(xtNA, na.print= "NA")## Create a nice display for the warp break data.warpbreaks$replicate <- rep_len(1:9, 54)ftable(xtabs(breaks ~ wool + tension + replicate, data = warpbreaks))### ---- Sparse Examples ----if(require("Matrix")) withAutoprint({ ## similar to "nlme"s 'ergoStool' : d.ergo <- data.frame(Type = paste0("T", rep(1:4, 9*4)), 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)})
xtabs: Cross Tabulation (2024)
Top Articles
Latest Posts
Article information

Author: Rob Wisoky

Last Updated:

Views: 5834

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Rob Wisoky

Birthday: 1994-09-30

Address: 5789 Michel Vista, West Domenic, OR 80464-9452

Phone: +97313824072371

Job: Education Orchestrator

Hobby: Lockpicking, Crocheting, Baton twirling, Video gaming, Jogging, Whittling, Model building

Introduction: My name is Rob Wisoky, I am a smiling, helpful, encouraging, zealous, energetic, faithful, fantastic person who loves writing and wants to share my knowledge and understanding with you.