2

How can I print latex-formatted tables (e.g. summary statistics, no regression output) in R, but without the environment code like \begin{table}[!htbp] \centering?

I tried many packages listed in Tools for making latex tables in R. Only the stargazer package seems to have an option for suppressing Latex environment code, namely out.header=FALSE. But that option seems to have no effect. Other packages seem to haven even less options.

Background: I have two table of very similar kind (spearman and pearson correlation, in my very case) which I would like to have in one table in Latex. I think of simply calling the R generated, latex-formatted output in a third latex file, which is ultimately called in the Latex document. But if there are other possibilities to create two R generated Latex-style tables in one .tex document, I'd be glad to use them.

Community
  • 1
  • 1
MERose
  • 4,048
  • 7
  • 53
  • 79

2 Answers2

7

You can do this with xtable:

Here's default behavior:

> print(xtable(table(1:5)))
% latex table generated in R 3.1.1 by xtable 1.7-4 package
% Sat Nov 08 14:57:56 2014
\begin{table}[ht]
\centering
\begin{tabular}{rr}
  \hline
 & V1 \\ 
  \hline
1 &   1 \\ 
  2 &   1 \\ 
  3 &   1 \\ 
  4 &   1 \\ 
  5 &   1 \\ 
   \hline
\end{tabular}
\end{table}

If you include floating = FALSE in the print method options, you can get your desired result:

> print(xtable(table(1:5)), floating = FALSE)
% latex table generated in R 3.1.1 by xtable 1.7-4 package
% Sat Nov 08 14:57:51 2014
\begin{tabular}{rr}
  \hline
 & V1 \\ 
  \hline
1 &   1 \\ 
  2 &   1 \\ 
  3 &   1 \\ 
  4 &   1 \\ 
  5 &   1 \\ 
   \hline
\end{tabular}

There's very fine-grained control here, but most of the options are described in ? print.xtable, not ? xtable.

Thomas
  • 43,637
  • 12
  • 109
  • 140
  • Now that I know the right option I realize that stargazer offers it too: `stargazer(..., float=FALSE)`. – MERose Nov 08 '14 at 15:47
0

Why don't you merge the two tables into one data.frame in a first step, and then format the combined thing for your task? There are only few details in your question but I imagine something along these lines here could work:

x <- rbind( cor(mtcars, use="complete.obs", method="spearman"),
            cor(mtcars, use="complete.obs", method="pearson" ))
rownames( x )[12:22] <- paste( rownames( x )[12:22], "a", sep = "-" )
xLatex <- xtable( x )
vaettchen
  • 7,299
  • 22
  • 41
  • Because I want to include a sub-headline (Panel A, Panel B). Additionally, the solution you posted relies on the definition of the rownames, which is relatively hard to generalize. – MERose Nov 08 '14 at 14:03
  • Some more details about the tables you want to combine would be helpful. – vaettchen Nov 08 '14 at 14:06