[R] Exporting Contingency Tables with xtable

Na'im R. Tyson NTYSON at CLOVERMAIL.NET
Wed Dec 9 06:04:36 CET 2009


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?




More information about the R-help mailing list