tableContinuous: Generate a LaTeX table of descriptive statistics for...

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

Description

Many data analyses start with a display of descriptive statistics of important variables. This function takes a data frame of continuous 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. User-defined statistics can be provided.

Usage

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

Arguments

vars

A data frame containing continuous 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) or a user-defined function. A user-defined function must bear a name, take a vector as an argument (NA's are removed by default) and return a single number (the desired statistic). For details see the examples below.

prec

Specify number of decimals to be displayed.

col.tit

Specify titles of columns. Note that the length of this vector must be equal to the length of stats plus the number of potential user-defined functions added to stats.

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 == "anova", p-values for an analysis of variance for a location difference between groups are added to the table. If print.pval == "kruskal", p-values of a Kruskal-Wallis test are given. If group has only two levels, the respective p-values of a t- or Mann-Whitney test are provided. Only applies if group is provided. Note that by default, any missing values are removed for computation of p-values. If missings should be considered a separate level, define the input variables accordingly.

pval.bound

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

declare.zero

Computed descriptive statistics (not p-values) below that constant are set to 0. Yields nicer tables, especially when displaying centered or standardized variables.

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 built of (some of) the default statistics character strings if not equal to NA. From package version 1.0.2 on 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 above.

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
23
24
25
26
27
28
29
30
31
data(CO2)
vars <- CO2[, 4:5]
group <- CO2[, "Treatment"]
weights <- c(rep(1, 60), rep(0, 10), rep(2, 14))

## display default statistics, provide neither group nor weights
tableContinuous(vars = vars, stats = c("n", "min", "mean", "median", 
    "max", "iqr", "na"), print.pval = "kruskal", 
    cap = "Table of continuous variables.", lab = "tab: descr stat")

## display default statistics, only use a subset of observations, grouped analysis
tableContinuous(vars = vars, weights = weights, subset = 
    c(rep(TRUE, 57), rep(FALSE, 100 - 57)), group = group, prec = 3, print.pval = 
    "kruskal", cap = "Table of continuous variables.", lab = "tab: descr stat")

## supply user-defined statistics: trimmed mean and IQR as an unbiased estimate 
## of the population standard deviation in case of normal data
my.stats <- list("n", "na", "mean", "$\\bar{x}_{trim}$" = function(x){return(mean(x, 
    trim = .05))}, "iqr", "IQR.unbiased" = function(x){return(IQR(x) / 
    (2 * qnorm(3 / 4)))})
tableContinuous(vars = vars, weights = weights, group = group, stats = my.stats, 
    prec = 3, print.pval = "none", cap = "Table of continuous variables.", 
    lab = "tab: descr stat")

## disp.cols and nams can still be used, for backward compatibility.
## 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).
tableContinuous(vars = list(CO2$conc, CO2$uptake, rnorm(1111), runif(2222)), 
    nams = c("conc", "uptake", "random1", "random2"), disp.cols = 
    c("n", "min", "median", "max", "iqr", "na"), cap = "Table of continuous variables.", lab = 
    "tab: descr stat")

Example output

Loading required package: xtable
% latex table generated in R 3.4.0 by xtable 1.8-2 package
% Wed Aug 23 19:54:52 2017
\begingroup\footnotesize
\begin{longtable}{lrrrrrrr}
 \textbf{Variable} & $\mathbf{n}$ & \textbf{Min} & $\mathbf{\bar{x}}$ & $\mathbf{\widetilde{x}}$ & \textbf{Max} & \textbf{IQR} & \textbf{\#NA} \\ 
  \hline
conc & 84 & 95.0 & 435.0 & 350.0 & 1000.0 & 500.0 & 0 \\ 
  uptake & 84 &  7.7 &  27.2 &  28.3 &   45.5 &  19.2 & 0 \\ 
  \hline
\caption{Table of continuous variables.} 
\label{tab: descr stat}
\end{longtable}
\endgroup
% latex table generated in R 3.4.0 by xtable 1.8-2 package
% Wed Aug 23 19:54:53 2017
\begingroup\footnotesize
\begin{longtable}{llrrrrrrrrrr}
 \textbf{Variable} & \textbf{Levels} & $\mathbf{n}$ & \textbf{Min} & $\mathbf{q_1}$ & $\mathbf{\widetilde{x}}$ & $\mathbf{\bar{x}}$ & $\mathbf{q_3}$ & \textbf{Max} & $\mathbf{s}$ & \textbf{IQR} & \textbf{\#NA} \\ 
  \hline
conc & nonchilled & 36 & 95.000 & 175.000 & 350.000 & 425.556 & 675.000 & 1000.000 & 299.566 & 500.000 & 0 \\ 
   & chilled & 21 & 95.000 & 175.000 & 350.000 & 435.000 & 675.000 & 1000.000 & 301.422 & 500.000 & 0 \\ 
   \hline
$p= 0.88$ & all & 57 & 95.000 & 175.000 & 350.000 & 429.035 & 675.000 & 1000.000 & 297.585 & 500.000 & 0 \\ 
   \hline
uptake & nonchilled & 36 & 10.600 &  27.025 &  32.400 &  31.375 &  39.850 &   45.500 &  10.234 &  12.825 & 0 \\ 
   & chilled & 21 &  9.300 &  27.300 &  35.000 &  31.752 &  38.700 &   42.400 &   9.645 &  11.400 & 0 \\ 
   \hline
$p= 0.92$ & all & 57 &  9.300 &  27.300 &  34.000 &  31.514 &  38.900 &   45.500 &   9.936 &  11.600 & 0 \\ 
   \hline
\hline
\caption{Table of continuous variables.} 
\label{tab: descr stat}
\end{longtable}
\endgroup
% latex table generated in R 3.4.0 by xtable 1.8-2 package
% Wed Aug 23 19:54:53 2017
\begingroup\footnotesize
\begin{longtable}{llrrrrrr}
 \textbf{Variable} & \textbf{Levels} & $\mathbf{n}$ & \textbf{\#NA} & $\mathbf{\bar{x}}$ & $\bar{x}_{trim}$ & \textbf{IQR} & IQR.unbiased \\ 
  \hline
conc & nonchilled & 39 & 0 & 412.692 & 405.405 & 412.500 & 305.787 \\ 
   & chilled & 49 & 0 & 435.000 & 425.000 & 500.000 & 370.651 \\ 
   \hline
 & all & 88 & 0 & 425.114 & 412.875 & 500.000 & 370.651 \\ 
   \hline
uptake & nonchilled & 39 & 0 &  30.836 &  30.986 &  13.450 &   9.970 \\ 
   & chilled & 49 & 0 &  22.020 &  21.773 &  21.000 &  15.567 \\ 
   \hline
 & all & 88 & 0 &  25.927 &  25.871 &  20.975 &  15.549 \\ 
   \hline
\hline
\caption{Table of continuous variables.} 
\label{tab: descr stat}
\end{longtable}
\endgroup
% latex table generated in R 3.4.0 by xtable 1.8-2 package
% Wed Aug 23 19:54:53 2017
\begingroup\footnotesize
\begin{longtable}{lrrrrrr}
 \textbf{Variable} & $\mathbf{n}$ & \textbf{Min} & $\mathbf{\widetilde{x}}$ & \textbf{Max} & \textbf{IQR} & \textbf{\#NA} \\ 
  \hline
conc &   84 & 95.0 & 350.0 & 1000.0 & 500.0 & 0 \\ 
  uptake &   84 &  7.7 &  28.3 &   45.5 &  19.2 & 0 \\ 
  random1 & 1111 & -3.6 &   0.0 &    3.3 &   1.4 & 0 \\ 
  random2 & 2222 &  0.0 &   0.5 &    1.0 &   0.5 & 0 \\ 
  \hline
\caption{Table of continuous variables.} 
\label{tab: descr stat}
\end{longtable}
\endgroup

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