displayCrossTabs: Function to display a set of K x C frequency tables,...

Description Usage Arguments Value Author(s) Examples

View source: R/displayCrossTabs.r

Description

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.

Usage

1
2
3
displayCrossTabs(vars, v0, nam0, lab0, 
    percentage = c("none", "row", "col", "total")[1], 
    add.p = TRUE)

Arguments

vars

Dataframe of nominal variables.

v0

Nominal variable to tabulate all columns of vars against.

nam0

Name of v0.

lab0

Initial string for table label. The column number of vars will be added, so that each table has a unique label.

percentage

Add percentages with respect to row, column, or table total.

add.p

Logical. If true, add p-value as described above.

Value

Displays LaTeX K x C tables and returns a list containing all the information.

Author(s)

Kaspar Rufibach (maintainer), kaspar.rufibach@gmail.com,
http://www.kasparrufibach.ch

Examples

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") 

Example output

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"

reporttools documentation built on Oct. 12, 2021, 5:06 p.m.