xtable | R Documentation |
Create a contingency table with totals, percentages, and statistical test.
xtable(
x,
by,
digits = 0L,
total = TRUE,
pct.sign = FALSE,
test = TRUE,
label.test = NULL,
html = FALSE,
...
)
x , by |
row and column variables, respectively; should be factor-like
and will be coerced; missing values in |
digits |
for percentages, number of digits past the decimal to keep |
total |
logical; if |
pct.sign |
logical; if |
test |
logical; if alternatively, a user-defined test function which takes two arguments,
|
label.test |
optional label for the test column |
html |
logical; if |
... |
additional arguments passed to
|
getDescriptionStatsBy
; tabler_stat2
;
rawr:::guess_test
x <- mtcars$gear
y <- mtcars$cyl
z <- mtcars$vs
table(x, y)
xtable(x, y)
xtable(ordered(x), z)
xtable(ordered(x), y)
xtable(ordered(x), ordered(y))
## user-defined test functions
test <- function(x, by) {
x <- fisher.test(table(x, by))
sprintf('%.1f (%.1f, %.1f), %s', x$estimate, x$conf.int[1],
x$conf.int[2], pvalr(x$p.value, show.p = TRUE))
}
xtable(mtcars$am, z, test = test, label.test = 'OR (95% CI), p-value')
test <- function(x, by) {
structure(
color_pval(chisq.test(table(x, by))$p.value),
name = '<i>p-value</i>'
)
}
res <- xtable(x, y, test = test)
res
names(dimnames(res)) <- c('Gears', 'Cylinders')
htmlTable::htmlTable(
res, n.cgroup = c(1, 3, 1), cgroup = c('', 'Cylinders', '')
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.