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.