Search code examples
rstatisticslatexknitrsweave

compareGroups table with knitr - how to adjust table head and subtext?


I just discovered the wonderful package compareGroups and I think this may help me a lot in the future. So far, it is working brilliantly in combination with Knitr/Sweave:

\documentclass{article}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% LaTeX packages needed for compareGroups

% long tables
\usepackage{longtable}

% multi row
\usepackage{multirow}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

% R code
<<results = "asis">>=

library(compareGroups)

# Very short code for bivariate descriptive table
tab <- compareGroups(vs ~ . , data = mtcars)
export2latex(createTable(tab))

@

\end{document}

produces enter image description here

I have only two concerns which is i) the modification of the table head (e.g. I want to print "P value" instead of "p.overall") and ii) adding a multicolumn subtext (e.g. "t-test was used to calculate P values."). If I just exported the table to LaTeX Code, it would be easy to modify head and subtext but in combination with Knitr I don't know how to deal with it. I just really would appreciate this wonderful solution over laborious functions in combination with e.g. Xtables package.

Thanks so much for your help!


Solution

  • Concern i) can be dealt with by manually reassigning the name of the 3rd column of matrix tab$descr prior to calling export2latex. Concern ii) might be addressed alternatively by placing the text in a caption (it seems to me more appropriate -- and further I have no idea about how to stick that into a multicol subheader).

    tab <- compareGroups(vs ~ . , data = mtcars)
    tab <- createTable(tab)
    colnames(tab$descr)[3] <- "P value"
    export2latex(tab, caption="$t$-test was used to calculate $p$ values.",
                 loc.caption="bottom")
    

    Regarding your request (below) to add, in fact, two captions (a numbered caption above the table and a non-numbered explanatory sub-text below the table), one way to achieve this is to encapsulate the longtable produced by export2latex within a (possibly centered) minipage environment, and placing the second caption as ordinary text below the table:

    \documentclass{article}
    \usepackage{longtable}
    \usepackage{multirow}
    
    \begin{document}
    
    See Table~\ref{tab:complicated} for the main results of this paper.
    \begin{center}
      \begin{minipage}[c]{0.5\textwidth}
    
    <<results = "asis", echo=FALSE>>=
    
    library(compareGroups)
    
    tab <- compareGroups(vs ~ . , data = mtcars)
    tab <- createTable(tab)
    colnames(tab$descr)[3] <- "P value"
    export2latex(tab, caption="Characteristics of the patients at baseline$^*$.",
                 label="tab:complicated"
                 )
    @
    {\footnotesize $^*$A two-sided $t$-test was used to calculate the $p$ values.}
    \end{minipage} 
    \end{center}
    
    \end{document}
    

    References:

    https://tex.stackexchange.com/questions/15282/tabular-title-above-and-caption-below