Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Print two tables with xtable ()

Tags:

r

sweave

I have data tables (d1 and d2) which I would like to print side by side or on top of each other in latex with their own individual titles. Is it possible to do that directly with xtable()? The two tables should be distinct, i.e. we could call them Table x(a) and Table x(b), but they should be either adjacent, or stacked.

like image 367
user702432 Avatar asked Jun 07 '11 09:06

user702432


1 Answers

I would recommend saving the results as two separate tables in different files (see the file= option to print.xtable()), and then input them into your LaTeX document with any command you find appropriate for your layout (tabular, subfloat, minipage, etc.). This is what I do in general, although I generally rely on LaTeX facilities in the Hmisc package. Should you want only to print them as a standalone PDF, use the standalone class for your document.

So, here is an example:

data(tli)
fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data=tli)
print(xtable(fm1), file="ta.tex", floating=FALSE)
print(xtable(head(tli, n=5)), file="tb.tex", floating=FALSE)

then, a quick tex wrapper (compile with pdflatex):

\documentclass{article}
\usepackage{subfig}
\usepackage{graphicx}

\begin{document}

\begin{table}[ht]
\centering
\subfloat[Table x(a)]{\label{tab:tab1a}\scalebox{.5}{\input{./ta}}}\quad
\subfloat[Table x(b)]{\label{tab:tab1b}\scalebox{.5}{\input{./tb}}}
\caption{Caption about here}
\label{tab:tab1}
\end{table}

\end{document}

Here is the result:

enter image description here

Remove the \scalebox command for default (stacked) layout, unless they are narrow enough to fit at their default size, as noted by @David.

enter image description here

like image 71
chl Avatar answered Sep 30 '22 03:09

chl