MERose
MERose

Reputation: 4421

R: Creating Latex tables without environment code

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.

Upvotes: 2

Views: 1212

Answers (2)

Thomas
Thomas

Reputation: 44527

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.

Upvotes: 7

vaettchen
vaettchen

Reputation: 7659

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 )

Upvotes: 0

Related Questions