ctable {summarytools}R Documentation

Cross-Tabulation

Description

Cross-tabulation for a pair of categorical variables with either row, column, or total proportions, as well as marginal sums. Works with numeric, character, as well as factor variables.

Usage

ctable(
  x,
  y,
  prop = st_options("ctable.prop"),
  useNA = "ifany",
  totals = st_options("ctable.totals"),
  style = st_options("style"),
  round.digits = st_options("ctable.round.digits"),
  justify = "right",
  plain.ascii = st_options("plain.ascii"),
  headings = st_options("headings"),
  display.labels = st_options("display.labels"),
  split.tables = Inf,
  dnn = c(substitute(x), substitute(y)),
  chisq = FALSE,
  OR = FALSE,
  RR = FALSE,
  weights = NA,
  rescale.weights = FALSE,
  ...
)

Arguments

x

First categorical variable - values will appear as row names.

y

Second categorical variable - values will appear as column names.

prop

Character. Indicates which proportions to show: “r” (rows, default), “c” (columns), “t” (total), or “n” (none). Default value can be changed using st_options, option ctable.prop.

useNA

Character. One of “ifany” (default), “no”, or “always”. This argument is passed on ‘as is’ to table, or adapted for xtabs when weights are used.

totals

Logical. Show row and column totals. Defaults to TRUE but can be set globally with st_options, option ctable.totals.

style

Character. Style to be used by pander. One of “simple” (default), “grid”, “rmarkdown”, or “jira”. Can be set globally with st_options.

round.digits

Numeric. Number of significant digits to keep. Defaults to 1. To change this default value, use st_options, option ctable.round.digits.

justify

Character. Horizontal alignment; one of “l” (left), “c” (center), or “r” (right, default).

plain.ascii

Logical. Used by pander; when TRUE, no markup characters are generated (useful when printing to console). Defaults to TRUE unless style = 'rmarkdown', in which case it is set to FALSE automatically. To change the default value globally, use st_options.

headings

Logical. Show heading section. TRUE by default; can be set globally with st_options.

display.labels

Logical. Display data frame label in the heading section. TRUE by default, can be changed globally with st_options.

split.tables

Numeric. pander argument that specifies how many characters wide a table can be. Inf by default.

dnn

Character vector. Variable names to be used in output table. In most cases, setting this parameter is not required as the names are automatically generated.

chisq

Logical. Display chi-square statistic along with p-value.

OR

Logical or numeric. Set to TRUE to show odds ratio with 95 confidence interval, or specify confidence level explicitly (e.g., .90). CI's are calculated using Wald's method of normal approximation.

RR

Logical or numeric. Set to TRUE to show risk ratio (also called relative risk with 95 confidence level explicitly (e.g. .90). CI's are calculated using Wald's method of normal approximation.

weights

Numeric. Vector of weights; must have the same length as x.

rescale.weights

Logical. When TRUE, a global constant is applied so that the sum of counts equals nrow(x). FALSE by default.

...

Additional arguments passed to pander or format.

Value

A list containing two matrices, cross_table and proportions. The print method takes care of assembling figures from those matrices into a single table. The returned object is of classes “summarytools” and “list”, unless stby is used, in which case we have an object of class “stby”.

Note

Markdown does not fully support multi-header tables; until such support is available, the recommended way to display cross-tables in .Rmd documents is to use 'method=render'. See package vignettes for examples.

Author(s)

Dominic Comtois, dominic.comtois@gmail.com

See Also

table, xtabs

Examples

data("tobacco")
ctable(tobacco$gender, tobacco$smoker)

# Use with() to simplify syntax
with(tobacco, ctable(smoker, diseased))

# Show column proportions, without totals
with(tobacco, ctable(smoker, diseased, prop = "c", totals = FALSE))

# Simple 2 x 2 table with odds ratio and risk ratio
with(tobacco, ctable(gender, smoker, totals = FALSE, headings = FALSE, prop = "n",
                     OR = TRUE, RR = TRUE))

# Grouped cross-tabulations
with(tobacco, stby(data = list(x = smoker, y = diseased), 
                   INDICES = gender, FUN = ctable))


## Not run: 
ct <- ctable(tobacco$gender, tobacco$smoker)

# Show html results in browser
print(ct, method = "browser")

# Save results to html file
print(ct, file = "ct_gender_smoker.html")

# Save results to text file
print(ct, file = "ct_gender_smoker.txt")

## End(Not run)

[Package summarytools version 1.0.1 Index]