Learn R Programming

SciencesPo (version 1.3.9)

crosstable: Cross-tabulation

Description

crosstable produces all possible two-way and three-way tabulations of variables.

Usage

crosstable(.data, ..., row = TRUE, column = TRUE, deparse.level = 2)

## S3 method for class 'default': crosstable(.data, ..., row = TRUE, column = TRUE, deparse.level = 2)

Arguments

.data
The data object.
row
TRUE.
column
TRUE.
deparse.level
Integer controlling the construction of labels in the case of non-matrix-like arguments. If 0, middle 2 rownames, if 1, 3 rownames, if 2, 4 rownames (default).
...
The variables for the cross tabulation.

Value

  • A cross-tabulated object.

encoding

UTF-8

See Also

xtabs, Frequency, table, prop.table

Examples

Run this code
titanic %>% crosstable( SEX, AGE, SURVIVED)

#' # Agresti (2002), table 3.10, p. 106
# 1992 General Social Survey--Race and Party affiliation
gss <- data.frame(
   expand.grid(Race=c("black", "white"),
   party=c("dem", "indep", "rep")),
   count=c(103,341,15,105,11,405))

df <- gss[rep(1:nrow(gss), gss[["count"]]), ]

crosstable(df, Race, party)

# Tea-Tasting Experiment data
 tea <- data.frame(
   expand.grid(poured=c("Yes", "No"),
   guess=c("Yes", "No")),
   count=c(3,1,1,3))

# nicer way of recreating long tables
data = untable(tea, freq="count")

crosstable(data, poured, guess, row=TRUE, column=TRUE) # fisher=TRUE

Run the code above in your browser using DataLab