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 |
useNA |
Character. One of “ifany” (default), “no”, or
“always”. This argument is passed on ‘as is’ to
|
totals |
Logical. Show row and column totals. Defaults to
|
style |
Character. Style to be used by |
round.digits |
Numeric. Number of significant digits to keep. Defaults
to |
justify |
Character. Horizontal alignment; one of “l” (left), “c” (center), or “r” (right, default). |
plain.ascii |
Logical. Used by |
headings |
Logical. Show heading section. |
display.labels |
Logical. Display data frame label in the heading
section. |
split.tables |
Numeric. |
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 |
RR |
Logical or numeric. Set to |
weights |
Numeric. Vector of weights; must have the same length as
|
rescale.weights |
Logical. When |
... |
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
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)