Description Usage Arguments Value Author(s) Examples
View source: R/displayCrossTabs.r
For each column of a dataframe, generate a LaTeX table against a given variable using displayKbyC
and add a suitable p-value:
If the expected frequencies are all >5 then a χ^2-test is computed, otherwise Fisher's exact test.
1 2 3 | displayCrossTabs(vars, v0, nam0, lab0,
percentage = c("none", "row", "col", "total")[1],
add.p = TRUE)
|
vars |
Dataframe of nominal variables. |
v0 |
Nominal variable to tabulate all columns of |
nam0 |
Name of |
lab0 |
Initial string for table label. The column number of |
percentage |
Add percentages with respect to row, column, or table total. |
add.p |
Logical. If true, add p-value as described above. |
Displays LaTeX K x C tables and returns a list containing all the information.
Kaspar Rufibach (maintainer), kaspar.rufibach@gmail.com,
http://www.kasparrufibach.ch
1 2 3 4 5 | set.seed(1977)
v0 <- round(runif(20, 0, 5))
v1 <- round(runif(20, 0, 3))
v2 <- round(runif(20, 0, 4))
displayCrossTabs(vars = data.frame(v1, v2), v0, nam0 = "v0", lab0 = "Q1")
|
Loading required package: xtable
% latex table generated in R 4.0.3 by xtable 1.8-4 package
% Tue May 25 23:18:42 2021
\begingroup\footnotesize
\begin{longtable}{ll|cccc|c}
& & v1 & & & & \\
& & 0 & 1 & 2 & 3 & Total \\
\hline
v0 & 0 & 0 & 1 & 0 & 0 & 1 \\
& 1 & 1 & 0 & 1 & 1 & 3 \\
& 2 & 1 & 1 & 3 & 1 & 6 \\
& 3 & 2 & 2 & 1 & 1 & 6 \\
& 4 & 1 & 0 & 0 & 0 & 1 \\
& 5 & 1 & 2 & 0 & 0 & 3 \\
\hline
& Total & 6 & 6 & 5 & 3 & 20 \\
\hline
\caption{v0 vs. v1. $p$-value Fisher's exact test: 0.91.}
\label{Q11}
\end{longtable}
\endgroup
% latex table generated in R 4.0.3 by xtable 1.8-4 package
% Tue May 25 23:18:42 2021
\begingroup\footnotesize
\begin{longtable}{ll|ccccc|c}
& & v2 & & & & & \\
& & 0 & 1 & 2 & 3 & 4 & Total \\
\hline
v0 & 0 & 0 & 0 & 0 & 1 & 0 & 1 \\
& 1 & 0 & 1 & 1 & 1 & 0 & 3 \\
& 2 & 2 & 1 & 2 & 1 & 0 & 6 \\
& 3 & 2 & 0 & 2 & 2 & 0 & 6 \\
& 4 & 1 & 0 & 0 & 0 & 0 & 1 \\
& 5 & 0 & 2 & 0 & 0 & 1 & 3 \\
\hline
& Total & 5 & 4 & 5 & 5 & 1 & 20 \\
\hline
\caption{v0 vs. v2. $p$-value Fisher's exact test: 0.58.}
\label{Q12}
\end{longtable}
\endgroup
$tabs
$tabs[[1]]
v2
v1 0 1 2 3
0 0 1 0 0
1 1 0 1 1
2 1 1 3 1
3 2 2 1 1
4 1 0 0 0
5 1 2 0 0
$tabs[[2]]
v2
v1 0 1 2 3 4
0 0 0 0 1 0
1 0 1 1 1 0
2 2 1 2 1 0
3 2 0 2 2 0
4 1 0 0 0 0
5 0 2 0 0 1
$ps
[1] 0.9075648 0.5809697
$tests
[1] "Fisher's exact" "Fisher's exact"
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.