tableDate: Display descriptive statistics for date variables

Description Usage Arguments Value Warning Note Author(s) References Examples

View source: R/tableDate.r

Description

Many data analyses start with a display of descriptive statistics of important variables. This function takes a data frame of date variables and possible grouping (such as e.g. treatment), weighting, and subset variables and provides a LaTeX table of descriptive statistics separately per group and jointly for all observations, per variable.

Usage

1
2
3
4
5
6
tableDate(vars, weights = NA, subset = NA, group = NA, 
     stats = c("n", "min", "q1", "median", "mean", "q3", "max", "na"), 
     col.tit = NA, col.tit.font = c("bf", "", "sf", "it", "rm"),
     print.pval = TRUE, pval.bound = 10^-4, cap = "", lab = "", 
     font.size = "footnotesize", longtable = TRUE, disp.cols = NA, 
     nams = NA, ...)

Arguments

vars

A data frame of date variables. See nams for an alternative way of specifying the variables to be displayed.

weights

Optional vector of weights of each observation.

subset

Optional logical vector, indicates subset of observations to be used.

group

Optional grouping variable.

stats

Specify which descriptive statistics should be displayed in the table, by either directly providing one or more of the default character strings (in arbitrary order).

col.tit

Specify titles of columns.

col.tit.font

If col.tit has not been specified, choose the font for the column titles here (default: no special font face).

print.pval

If print.pval == TRUE, p-values of a Mann-Whitney or Kruskal-Wallis test for a difference between groups are provided.

pval.bound

p-values below pval.bound are formatted as < pval.bound.

cap

The caption of the resulting LaTeX table.

lab

The label of the resulting LaTeX table.

font.size

Font size for the generated table in LaTeX.

longtable

If TRUE, function makes use of package longtable in LaTex to generate tables that span more than one page. If FALSE, generates a table in tabular environment.

disp.cols

Only included for backward compatibility. Needs to be a vector of (some of) the default statistics character strings if not equal to NA. From package version 1.0.2 use of stats is recommended.

nams

A vector of strings, containing the names corresponding to the variables in vars, if vars is not a data frame but a list of variables. These are then the names that appear in the LaTeX table. This option is only kept for backward compatibility.

...

Arguments pass through to print.xtable.

Value

Outputs the LaTeX table.

Warning

If either one of the arguments group, weights, or subset is different from NA and if vars is a list, then it is assumed that all variables in vars are of equal length.

Note

If longtable = TRUE (which is the default), the function generates a table that may be more than one page long, you need to include the package longtable in the LaTeX source.

If a list of variables is given to vars, not all of these variables need to be of the same length. However, note the Warning below.

Author(s)

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

References

Rufibach, K. (2009) reporttools: R-Functions to Generate LaTeX Tables of Descriptive Statistics. Journal of Statistical Software, Code Snippets, 31(1).
doi: 10.18637/jss.v031.c01.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
set.seed(1977)
diagnosis <- as.Date(round(runif(10, min = 35000, max = 40000)), 
    origin = "1899-12-30")
death <- as.Date(round(runif(10, min = 35000, max = 40000)), 
    origin = "1899-12-30")
vars <- data.frame(diagnosis, death)
group <- sample(c(rep("A", 5), rep("B", 5)))
tableDate(vars = vars, group = group, stats = c("n", "min", "median", "max", "na"), 
    cap = "Table of date variables.", lab = "tab: descr stat date")
    
## suppose we have weighted observations
weights <- c(2, 3, 1, 4, rep(1, 6))
subset <- 1:5
tableDate(vars = vars, weights = weights, subset = subset, 
    cap = "Table of date variables.", lab = "tab: descr stat date")

## For backward compatibility, disp.cols and nams are still working.
## If a list is given to vars, the variables can be of different length. 
## However, then weights, subset, and group must be set to NA (the default).
tableDate(vars = list(diagnosis, death), nams = c("Diagnosis", "Death"), 
    disp.cols = c("n", "na", "min", "max"), print.pval = FALSE, cap = 
    "Table of date variables.", lab = "tab: descr stat date")    

Example output

Loading required package: xtable
% latex table generated in R 3.4.4 by xtable 1.8-4 package
% Mon Jul  1 16:01:20 2019
\begingroup\footnotesize
\begin{longtable}{llrrrrr}
 \textbf{Variable} & \textbf{Levels} & $\mathbf{n}$ & \textbf{Min} & $\mathbf{\widetilde{x}}$ & \textbf{Max} & \textbf{\#NA} \\ 
  \hline
diagnosis & A &   5 & 1998-10-05 & 11558 & 2008-07-13 &   0 \\ 
   & B &   5 & 1997-09-17 & 11537 & 2005-04-14 &   0 \\ 
   \hline
$p= 0.75$ & all &  10 & 1997-09-17 & 11547.5 & 2008-07-13 &   0 \\ 
   \hline
\hline
death & A &   5 & 1999-11-08 & 12670 & 2009-03-10 &   0 \\ 
   & B &   5 & 1996-03-06 & 12106 & 2005-09-11 &   0 \\ 
   \hline
$p= 0.25$ & all &  10 & 1996-03-06 & 12520.5 & 2009-03-10 &   0 \\ 
   \hline
\hline
\hline
\caption{Table of date variables.} 
\label{tab: descr stat date}
\end{longtable}
\endgroup
Warning messages:
1: In kruskal.test.default(splits) :
  some elements of 'x' are not numeric and will be coerced to numeric
2: In kruskal.test.default(splits) :
  some elements of 'x' are not numeric and will be coerced to numeric
% latex table generated in R 3.4.4 by xtable 1.8-4 package
% Mon Jul  1 16:01:20 2019
\begingroup\footnotesize
\begin{longtable}{lrrrrrrrr}
  \hline
\textbf{Variable} & $\mathbf{n}$ & \textbf{Min} & $\mathbf{q_1}$ & $\mathbf{\widetilde{x}}$ & $\mathbf{\bar{x}}$ & $\mathbf{q_3}$ & \textbf{Max} & \textbf{\#NA} \\ 
  \hline
diagnosis &  11 & 1997-09-17 & 10121 & 11914 & 2001-02-22 & 12125 & 2005-04-14 &   0 \\ 
  death &  11 & 1996-03-06 & 11908 & 11908 & 2003-10-14 & 14186 & 2009-03-10 &   0 \\ 
   \hline
\hline
\caption{Table of date variables.} 
\label{tab: descr stat date}
\end{longtable}
\endgroup
% latex table generated in R 3.4.4 by xtable 1.8-4 package
% Mon Jul  1 16:01:20 2019
\begingroup\footnotesize
\begin{longtable}{lrrrr}
  \hline
\textbf{Variable} & $\mathbf{n}$ & \textbf{\#NA} & \textbf{Min} & \textbf{Max} \\ 
  \hline
Diagnosis &  10 &   0 & 1997-09-17 & 2008-07-13 \\ 
  Death &  10 &   0 & 1996-03-06 & 2009-03-10 \\ 
   \hline
\hline
\caption{Table of date variables.} 
\label{tab: descr stat date}
\end{longtable}
\endgroup

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