crosstab: Cross Tabulation

View source: R/crosstab.R

crosstabR Documentation

Cross Tabulation

Description

This function creates a two-way and three-way cross tabulation with absolute frequencies and row-wise, column-wise and total percentages.

Usage

crosstab(data, ..., print = c("no", "all", "row", "col", "total"),
         freq = TRUE, split = FALSE, na.omit = TRUE, digits = 2, as.na = NULL,
         write = NULL, append = TRUE, check = TRUE, output = TRUE)

Arguments

data

a data frame with two or three columns.

...

an expression indicating the variable names in data, e.g., crosstab(dat, x1, x2, x3). Note that the operators +, -, ~, :, ::, and ! can also be used to select variables, see 'Details' in the df.subset function.

print

a character string or character vector indicating which percentage(s) to be printed on the console, i.e., no percentages ("no") (default), all percentages ("all"), row-wise percentages ("row"), column-wise percentages ("col"), and total percentages ("total").

freq

logical: if TRUE (default), absolute frequencies will be included in the cross tabulation.

split

logical: if TRUE, output table is split in absolute frequencies and percentage(s).

na.omit

logical: if TRUE (default), incomplete cases are removed before conducting the analysis (i.e., listwise deletion).

digits

an integer indicating the number of decimal places digits to be used for displaying percentages.

as.na

a numeric vector indicating user-defined missing values, i.e. these values are converted to NA before conducting the analysis.

write

a character string naming a file for writing the output into either a text file with file extension ".txt" (e.g., "Output.txt") or Excel file with file extension ".xlsx" (e.g., "Output.xlsx"). If the file name does not contain any file extension, an Excel file will be written.

append

logical: if TRUE (default), output will be appended to an existing text file with extension .txt specified in write, if FALSE existing text file will be overwritten.

check

logical: if TRUE (default), argument specification is checked.

output

logical: if TRUE (default), output is printed on the console.

Value

Returns an object of class misty.object, which is a list with following entries:

call

function call

type

type of analysis

data

data frame specified in data

args

specification of function arguments

result

list with result tables, i.e., crosstab for the cross tabulation, freq.a for the absolute frequencies, perc.r for the row-wise percentages, perc.c for the column-wise percentages, perc.t for the total percentages

Author(s)

Takuya Yanagida takuya.yanagida@univie.ac.at

write.result, freq, descript, multilevel.descript, na.descript.

References

Rasch, D., Kubinger, K. D., & Yanagida, T. (2011). Statistics in psychology - Using R and SPSS. John Wiley & Sons.

Examples

#————————————————————————————————————————————————————————————————————————————
# Two-Dimensional Table

# Example 1a: Cross Tabulation for 'vs' and 'am'
crosstab(mtcars, vs, am)

# Alternative specification without using the '...' argument
crosstab(mtcars[, c("vs", "am")])

# Example 1b: Cross Tabulation, print all percentages
crosstab(mtcars, vs, am, print = "all")

# Example 1c: Cross Tabulation, print row-wise percentages
crosstab(mtcars, vs, am, print = "row")

# Example 1d: Cross Tabulation, print col-wise percentages
crosstab(mtcars, vs, am, print = "col")

# Example 1e: Cross Tabulation, print all percentages, split output table
crosstab(mtcars, vs, am, print = "all", split = TRUE)

#————————————————————————————————————————————————————————————————————————————
# Three-Dimensional Table

# Example 2a: Cross Tabulation for 'vs', 'am', ane 'gear'
crosstab(mtcars, vs:gear)

# Alternative specification without using the '...' argument
crosstab(mtcars[, c("vs", "am", "gear")])

# Example 2b: Cross Tabulation, print all percentages
crosstab(mtcars, vs:gear, print = "all")

# Example 2c: Cross Tabulation, print all percentages, split output table
crosstab(mtcars, vs:gear, print = "all", split = TRUE)

## Not run: 
#————————————————————————————————————————————————————————————————————————————
# Write Results

# Example 3a: Write Results into a text file
crosstab(mtcars, vs:gear, print = "all", write = "Crosstab.txt")

# Example 3b: Write Results into an Excel file
crosstab(mtcars, vs:gear, print = "all", write = "Crosstab.xlsx")

## End(Not run)

misty documentation built on Aug. 2, 2026, 9:06 a.m.

Related to crosstab in misty...