Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export a table from R to latex and include dimension names?

Tags:

r

latex

I have a simple table with dimension names that I want to export from R to Latex. I'm looking for a straightforward way to do this that doesn't requiring additional editing in latex. This seems like it should be easy, but I've found a number of other unsolved questions on this topic.

I've tried to use the Hmisc latex command following this post. It yields ! LaTeX Error: Illegal character in array arg. as the OP states, but the question remains unresolved. This post has a pile of table options, but I don't see that dimension names are addressed.

R-code

library(Hmisc) 
latex(table(state.division, state.region), rowlabel = "X", collabel =  "Y", file = "") 

Output

%latex.default(table(state.division, state.region), rowlabel = "X",     collabel = "Y", file = "")%
\begin{table}[!tbp]
\begin{center}
\begin{tabular}{lrrrr}
\hline\hline
\multicolumn{1}{l}{X}&\multicolumn{1}{Y}{Northeast}&\multicolumn{1}{l}{South}&\multicolumn{1}{Y}{North Central}&\multicolumn{1}{l}{West}\tabularnewline
\hline
New England&$6$&$0$&$0$&$0$\tabularnewline
Middle Atlantic&$3$&$0$&$0$&$0$\tabularnewline
South Atlantic&$0$&$8$&$0$&$0$\tabularnewline
East South Central&$0$&$4$&$0$&$0$\tabularnewline
West South Central&$0$&$4$&$0$&$0$\tabularnewline
East North Central&$0$&$0$&$5$&$0$\tabularnewline
West North Central&$0$&$0$&$7$&$0$\tabularnewline
Mountain&$0$&$0$&$0$&$8$\tabularnewline
Pacific&$0$&$0$&$0$&$5$\tabularnewline
\hline
\end{tabular}\end{center}

\end{table}

Error Messages After Latex Rendering

Errors:

./test.tex:9: LaTeX Error: Illegal character in array arg. [...lumn{1}{l}{X}&\multicolumn{1}{Y}{Northeast}]
./test.tex:9: LaTeX Error: Illegal character in array arg. [...l}{South}&\multicolumn{1}{Y}{North Central}]

Addressing the errors by adding the alignment &\multicolumn{1}{l}{Y}{Northeast} yields a table that doesn't have the dimension names in the appropriate place.

Desired Output

                    state.region
_____________________________________________________________
state.division       Northeast South North Central West
_____________________________________________________________
  New England                6     0             0    0
  Middle Atlantic            3     0             0    0
  South Atlantic             0     8             0    0
  East South Central         0     4             0    0
  West South Central         0     4             0    0
  East North Central         0     0             5    0
  West North Central         0     0             7    0
  Mountain                   0     0             0    8
  Pacific                    0     0             0    5

Also tried using the memisc package, which similarly yields Illegal character errors.

like image 709
Minnow Avatar asked Feb 27 '15 17:02

Minnow


Video Answer


1 Answers

You can use xtable for this (from the xtable package):

# create your table
tab <- table(state.division, state.region)

# reassemble to put things where they need to be
tab2 <- cbind(rownames(tab), tab)
tab3 <- rbind(c("","\\multicolumn{4}{l}{state.region}", rep("",ncol(tab2)-2)),
              c("state.division",colnames(tab2)[-1]), 
              tab2)

# print as xtable
library("xtable")
print(xtable(tab3), include.rownames = FALSE, include.colnames = FALSE, sanitize.text.function = I, hline.after = c(0,1,2,11))

You can use the file argument in print.xtable if you want to write this directly to a file. Here's the resulting LaTeX code:

% latex table generated in R 3.1.3 by xtable 1.7-4 package
% Thu Apr 23 21:25:44 2015
\begin{table}[ht]
\centering
\begin{tabular}{lllll}
   \hline
 & \multicolumn{4}{l}{state.region} &  &  &  \\ 
   \hline
state.division & Northeast & South & North Central & West \\ 
   \hline
New England & 6 & 0 & 0 & 0 \\ 
  Middle Atlantic & 3 & 0 & 0 & 0 \\ 
  South Atlantic & 0 & 8 & 0 & 0 \\ 
  East South Central & 0 & 4 & 0 & 0 \\ 
  West South Central & 0 & 4 & 0 & 0 \\ 
  East North Central & 0 & 0 & 5 & 0 \\ 
  West North Central & 0 & 0 & 7 & 0 \\ 
  Mountain & 0 & 0 & 0 & 8 \\ 
  Pacific & 0 & 0 & 0 & 5 \\ 
   \hline
\end{tabular}
\end{table}

And the PDF result:

enter image description here

like image 142
Thomas Avatar answered Oct 24 '22 18:10

Thomas