[R] Exporting Contingency Tables with xtable

Na'im R. Tyson NTYSON at clovermail.net
Thu Dec 10 01:18:06 CET 2009


Gabor,

Thanks for the advice.  Using the 'rowlabel' switch works, but when  
used with the 'collabel' switch, I received the following error in  
Latex 2e:

! LaTeX Error: Illegal character in array arg.

The line that LaTeX has issue with is:

\multicolumn{1}{observed}{uh}&

The entire table looks like:

\begin{table}[!tbp]
  \begin{center}
  \begin{tabular}{lrr}\hline\hline
\multicolumn{1}{l}{predicted}&
\multicolumn{1}{observed}{uh}&
\multicolumn{1}{l}{uh~}
\tabularnewline \hline
uh&$201$&$30$\tabularnewline
uh~&$  6$&$10$\tabularnewline
\hline
\end{tabular}
\end{center}
\end{table}

I know that LaTeX has issues with unescaped tildes, but it does not  
explain why I get this error.

Na'im

On Dec 9, 2009, at 5:44 AM, Gabor Grothendieck wrote:

> Try the latex function in the Hmisc package.  Using the state.*
> variables built into R for sake of example:
>
> library(Hmisc)
> latex(table(state.division, state.region), rowlabel = "X", collabel =
> "Y", file = "")
>
>
>
> On Wed, Dec 9, 2009 at 12:04 AM, Na'im R. Tyson  
> <NTYSON at clovermail.net> wrote:
>> Dear R-philes:
>>
>> I am having an issue with exporting contingency tables with  
>> xtable().  I set
>> up a contingency and convert it to a matrix for passing to xtable()  
>> as shown
>> below.
>>
>> v.cont.table <- table(v_lda$class, grps,
>>        dnn=c("predicted", "observed"))
>> v.cont.mat <- as.matrix(v.cont.table)
>>
>> Both produce output as follows:
>>
>>                observed
>> predicted  uh uh~
>>      uh  201  30
>>      uh~   6  10
>>
>> However, when I construct the latex table with xtable(v.cont.mat),  
>> I get a
>> good table without the headings of "predicted" and "observed".
>>
>> \begin{table}[ht]
>> \begin{center}
>> \begin{tabular}{rrr}
>>  \hline
>>  & uh & uh\~{} \\
>>  \hline
>> uh & 201 &  30 \\
>>  uh\~{} &   6 &  10 \\
>>   \hline
>> \end{tabular}
>> \end{center}
>> \end{table}
>>
>> Question: is there any easy way to retain or re-insert the  
>> dimension names
>> from the contingency table and matrix?
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>




More information about the R-help mailing list