summarytools (version 0.9.9)

ctable: Cross-Tabulation

Description

Cross-tabulation for a pair of categorical variables (or factors) with either row, column, or total proportions, as well as marginal sums.

Usage

ctable(
  x,
  y,
  prop = st_options("ctable.prop"),
  useNA = "ifany",
  totals = st_options("ctable.totals"),
  style = st_options("style"),
  round.digits = 1,
  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 in as column names.

prop

Proportions to display; “r” for rows (default), “c” for columns, “t” for total, or “n” for none. This option can be set globally; see st_options.

useNA

Argument passed on to table; One of “ifany” (default), “no”, or “always”.

totals

Logical. Should row and column totals be displayed? Defaults to TRUE. To change this default value globally, see st_options.

style

Style to be used by pander when rendering output table; One of “simple” (default), “grid”, or “rmarkdown”. This option can be set globally; see st_options.

round.digits

Number of significant digits to display. Defaults to 1. To change this default value globally, see st_options.

justify

String indicating alignment of columns; one of “l” (left) “c” (center), or “r” (right). Defaults to “r”.

plain.ascii

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

headings

Logical. Set to FALSE to omit heading section. Can be set globally via st_options.

display.labels

Logical. Should variable / data frame label be displayed in the title section? Default is TRUE. To change this default value globally, use st_options.

split.tables

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

dnn

Names to be used in output table. Vector of two strings; By default, the character values for arguments x and y are used.

chisq

Logical. Display chisq statistic along with p-value.

OR

Logical or numeric. Display odds ratio with the specified confidence level (typically .95). Can be set to TRUE, in which case 95 interval is given. Confidence intervals are calculated using Wald's method (normal approximation).

RR

Logical or numeric. Display risk ratio (also called relative risk) with the specified confidence level (typically .95). Can be set to TRUE, in which case 95 calculated using Wald's method (normal approximation).

weights

Vector of weights; must be of the same length as x.

rescale.weights

Logical parameter. When set to TRUE, the total count will be the same as the unweighted x. FALSE by default.

Additional arguments passed to pander.

Value

A frequency table of classes matrix and summarytools with added attributes used by print method.

Details

Rmarkdown does not, to this day, support multi-header tables. Therefore, until such support is available, the recommended way to display cross-tables in .Rmd documents is to use `method=render` with the `print()` or `view()` functions. See package vignettes for examples.

See Also

table, xtabs

Examples

Run this code
# NOT RUN {
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(list(x = smoker, y = diseased), gender, 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")
# }

Run the code above in your browser using DataLab