latex_table_fac: Produce (LaTeX) Summaries for Factor Variables

Description Usage Arguments Details Value Author(s) See Also Examples

Description

The function produces LaTeX tables with summary statistics for factor variables. It makes use of the booktabs package in LaTeX to obtain tables with a nice layout.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
latex.table.fac(data, variables = names(data), labels = NULL, group = NULL,
                colnames = NULL, digits = 2, table = c("tabular", "longtable"),
                align = NULL, caption = NULL, label = NULL, floating = FALSE,
                center = TRUE, sep = TRUE, sanitize = TRUE, drop = TRUE,
                show.NAs = any(is.na(data[, variables])), na.lab = "<Missing>")

## S3 method for class 'table.fac'
print(x,
      colnames = get_options(x, "colnames"),
      table = get_options(x, "table"),
      align = get_options(x, "align"),
      caption = get_options(x, "caption"),
      label = get_options(x, "label"),
      floating = get_options(x, "floating"),
      center = get_options(x, "center"),
      sep = get_options(x, "sep"),
      sanitize = get_options(x, "sanitize"),
      ...)

Arguments

data

data set to be used

variables

character vector defining (factor) variables that should be included in the table. Per default, all factor variables of data are used.

labels

labels for the variables. If labels = NULL (default) variables is used as label. If labels = TRUE, labels(data, which = variables) is used as labels.

group

character specifying a grouping factor. Per default no grouping is applied.

colnames

a vector of character strings of length 4 (variable, label, N, and fraction in %). The vector supplies alternative column names for the resulting LaTeX table. If NULL default names are used.

digits

number of digits to round to (only used for fractions).

table

character string. LaTeX table format, currently either "tabular" (default) or "longtable".

align

character string. LaTeX alignment of table rows, per default "llr...r", where "r" is repeated ncol - 2 times.

caption

(optional) character string. Caption of LaTeX table. Note that captions are suported for all tables (see also details below).

label

(optional) character string. Label of LaTeX table specified as \label{"label"}.

floating

logical (default: FALSE). Determines wether the table is a floating object (i.e. use a table environment or not). Note that a longtable cannot be a floating object but captions can be used.

center

logical (default: TRUE). Determines if table should be centered.

sep

logical (default: TRUE). Determines wether separators (lines) should be added after each factor variable.

sanitize

logical (default: TRUE) or a sanitizing function used to clean the input in order to be useable in LaTeX environments. Per default toLatex.character is used.

drop

logical (default: TRUE). Determines wether factor variables, which contain only missing values are dropped from the table.

show.NAs

logical. Determines if NAs are displayed as a separate category for each factor variable with missings. If TRUE, an additional statistic which includes the missings is displayed (see Examples). Per default, show.NAs is TRUE if there are any missings in the variables to be displayed (and FALSE if not).

na.lab

label for missing values (default: "<Missing>").

x

An object of class table.fac. See Value for details.

...

additional arguments for printing. Currently not used.

Details

The output requires \usepackage{booktabs} in the LaTeX file.

Captions can be added to both, longtables and tabulars. In the latter case, captions are also suported if the table is no floating object. In this case, the LaTeX package capt-of is required.

Value

A special data.frame with additional class table.fac containing the computed statistics is returned from function latex.table.cont. Addtional attributes required for the print function are contained (see attr(x, "latex.table.options")). These are extracted in the print function using the function get_options.

The output is printed with LaTeX style syntax highlighting to be used e.g. in Sweave chunks with results=tex.

Author(s)

Benjamin Hofner

See Also

latex.table.cont and get_options

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
## Example requires package nlme to be installed and loaded
if (require("nlme")) {
    ## Use dataset Orthodont
    data(Orthodont, package = "nlme")

    ## Get summary for continuous variables
    latex.table.fac(Orthodont)

    ## Reorder data for table:
    latex.table.fac(Orthodont, variables = c("Sex", "Subject"))

    ## What happens in the display if we introduce some missing values:
    set.seed(1907)
    Orthodont$Sex[sample(nrow(Orthodont), 20)] <- NA
    latex.table.fac(Orthodont)
    latex.table.fac(Orthodont, variables = "Sex")
    ## do not show statistics on missing values
    latex.table.fac(Orthodont, variables = "Sex", show.NAs = FALSE)
}

Example output

Loading required package: car
Loading required package: carData
Loading required package: xtable

Attaching package: 'papeR'

The following object is masked from 'package:utils':

    toLatex

Loading required package: nlme
Non-factors are dropped from the summary
NOTE: Output requires \usepackage{booktabs} in your preamble.
\begin{center}
% latex table generated in R 3.4.4 by xtable 1.8-4 package
% Mon Nov  2 14:32:34 2020
\begin{tabular}{llrrr}
  \toprule
   & Level &   & N & \% \\ 
    \cmidrule{2-2} \cmidrule{4-5} 
 Subject & M16 &  &   4 & 3.7 \\ 
   & M05 &  &   4 & 3.7 \\ 
   & M02 &  &   4 & 3.7 \\ 
   & M11 &  &   4 & 3.7 \\ 
   & M07 &  &   4 & 3.7 \\ 
   & M08 &  &   4 & 3.7 \\ 
   & M03 &  &   4 & 3.7 \\ 
   & M12 &  &   4 & 3.7 \\ 
   & M13 &  &   4 & 3.7 \\ 
   & M14 &  &   4 & 3.7 \\ 
   & M09 &  &   4 & 3.7 \\ 
   & M15 &  &   4 & 3.7 \\ 
   & M06 &  &   4 & 3.7 \\ 
   & M04 &  &   4 & 3.7 \\ 
   & M01 &  &   4 & 3.7 \\ 
   & M10 &  &   4 & 3.7 \\ 
   & F10 &  &   4 & 3.7 \\ 
   & F09 &  &   4 & 3.7 \\ 
   & F06 &  &   4 & 3.7 \\ 
   & F01 &  &   4 & 3.7 \\ 
   & F05 &  &   4 & 3.7 \\ 
   & F07 &  &   4 & 3.7 \\ 
   & F02 &  &   4 & 3.7 \\ 
   & F08 &  &   4 & 3.7 \\ 
   & F03 &  &   4 & 3.7 \\ 
   & F04 &  &   4 & 3.7 \\ 
   & F11 &  &   4 & 3.7 \\ 
     \cmidrule{2-2} \cmidrule{4-5} 
Sex & Male &  &  64 & 59.3 \\ 
   & Female &  &  44 & 40.7 \\ 
   \bottomrule
\end{tabular}
\end{center}
  This function exists for backward compatibility.
  Consider using 'xtable(summarize(..., type = "factor"))' instead.
NOTE: Output requires \usepackage{booktabs} in your preamble.
\begin{center}
% latex table generated in R 3.4.4 by xtable 1.8-4 package
% Mon Nov  2 14:32:35 2020
\begin{tabular}{llrrr}
  \toprule
   & Level &   & N & \% \\ 
    \cmidrule{2-2} \cmidrule{4-5} 
 Sex & Male &  &  64 & 59.3 \\ 
   & Female &  &  44 & 40.7 \\ 
     \cmidrule{2-2} \cmidrule{4-5} 
Subject & M16 &  &   4 & 3.7 \\ 
   & M05 &  &   4 & 3.7 \\ 
   & M02 &  &   4 & 3.7 \\ 
   & M11 &  &   4 & 3.7 \\ 
   & M07 &  &   4 & 3.7 \\ 
   & M08 &  &   4 & 3.7 \\ 
   & M03 &  &   4 & 3.7 \\ 
   & M12 &  &   4 & 3.7 \\ 
   & M13 &  &   4 & 3.7 \\ 
   & M14 &  &   4 & 3.7 \\ 
   & M09 &  &   4 & 3.7 \\ 
   & M15 &  &   4 & 3.7 \\ 
   & M06 &  &   4 & 3.7 \\ 
   & M04 &  &   4 & 3.7 \\ 
   & M01 &  &   4 & 3.7 \\ 
   & M10 &  &   4 & 3.7 \\ 
   & F10 &  &   4 & 3.7 \\ 
   & F09 &  &   4 & 3.7 \\ 
   & F06 &  &   4 & 3.7 \\ 
   & F01 &  &   4 & 3.7 \\ 
   & F05 &  &   4 & 3.7 \\ 
   & F07 &  &   4 & 3.7 \\ 
   & F02 &  &   4 & 3.7 \\ 
   & F08 &  &   4 & 3.7 \\ 
   & F03 &  &   4 & 3.7 \\ 
   & F04 &  &   4 & 3.7 \\ 
   & F11 &  &   4 & 3.7 \\ 
   \bottomrule
\end{tabular}
\end{center}
  This function exists for backward compatibility.
  Consider using 'xtable(summarize(..., type = "factor"))' instead.
Non-factors are dropped from the summary
NOTE: Output requires \usepackage{booktabs} in your preamble.
\begin{center}
% latex table generated in R 3.4.4 by xtable 1.8-4 package
% Mon Nov  2 14:32:35 2020
\begin{tabular}{llrrr}
  \toprule
   & Level &   & N & \% \\ 
    \cmidrule{2-2} \cmidrule{4-5} 
 Subject & M16 &  &   4 & 3.7 \\ 
   & M05 &  &   4 & 3.7 \\ 
   & M02 &  &   4 & 3.7 \\ 
   & M11 &  &   4 & 3.7 \\ 
   & M07 &  &   4 & 3.7 \\ 
   & M08 &  &   4 & 3.7 \\ 
   & M03 &  &   4 & 3.7 \\ 
   & M12 &  &   4 & 3.7 \\ 
   & M13 &  &   4 & 3.7 \\ 
   & M14 &  &   4 & 3.7 \\ 
   & M09 &  &   4 & 3.7 \\ 
   & M15 &  &   4 & 3.7 \\ 
   & M06 &  &   4 & 3.7 \\ 
   & M04 &  &   4 & 3.7 \\ 
   & M01 &  &   4 & 3.7 \\ 
   & M10 &  &   4 & 3.7 \\ 
   & F10 &  &   4 & 3.7 \\ 
   & F09 &  &   4 & 3.7 \\ 
   & F06 &  &   4 & 3.7 \\ 
   & F01 &  &   4 & 3.7 \\ 
   & F05 &  &   4 & 3.7 \\ 
   & F07 &  &   4 & 3.7 \\ 
   & F02 &  &   4 & 3.7 \\ 
   & F08 &  &   4 & 3.7 \\ 
   & F03 &  &   4 & 3.7 \\ 
   & F04 &  &   4 & 3.7 \\ 
   & F11 &  &   4 & 3.7 \\ 
     \cmidrule{2-2} \cmidrule{4-5} 
Sex & Male &  &  51 & 47.2 \\ 
   & Female &  &  37 & 34.3 \\ 
   & $<$Missing$>$ &  &  20 & 18.5 \\ 
   \bottomrule
\end{tabular}
\end{center}
  This function exists for backward compatibility.
  Consider using 'xtable(summarize(..., type = "factor"))' instead.
NOTE: Output requires \usepackage{booktabs} in your preamble.
\begin{center}
% latex table generated in R 3.4.4 by xtable 1.8-4 package
% Mon Nov  2 14:32:35 2020
\begin{tabular}{llrrr}
  \toprule
   & Level &   & N & \% \\ 
    \cmidrule{2-2} \cmidrule{4-5} 
 Sex & Male &  &  51 & 47.2 \\ 
   & Female &  &  37 & 34.3 \\ 
   & $<$Missing$>$ &  &  20 & 18.5 \\ 
   \bottomrule
\end{tabular}
\end{center}
  This function exists for backward compatibility.
  Consider using 'xtable(summarize(..., type = "factor"))' instead.
NOTE: Output requires \usepackage{booktabs} in your preamble.
\begin{center}
% latex table generated in R 3.4.4 by xtable 1.8-4 package
% Mon Nov  2 14:32:35 2020
\begin{tabular}{llrrr}
  \toprule
   & Level &   & N & \% \\ 
    \cmidrule{2-2} \cmidrule{4-5} 
 Sex & Male &  &  51 & 58.0 \\ 
   & Female &  &  37 & 42.0 \\ 
   \bottomrule
\end{tabular}
\end{center}
  This function exists for backward compatibility.
  Consider using 'xtable(summarize(..., type = "factor"))' instead.

papeR documentation built on May 2, 2019, 4:55 p.m.