Reputation: 777
I would like xtable to NOT over-ride my duplicated rowname entries. Consider the following MWE:
> require(xtable)
> foo <- matrix(0, 2,2)
> rownames(foo) = rep("bar", 2)
> foo
[,1] [,2]
bar 0 0
bar 0 0
> xtable(foo)
% latex table generated in R 2.15.0 by xtable 1.7-0 package
% Fri Jun 22 13:59:36 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrr}
\hline
& 1 & 2 \\
\hline
1 & 0.00 & 0.00 \\
2 & 0.00 & 0.00 \\
\hline
\end{tabular}
\end{center}
\end{table}
Warning message:
In data.row.names(row.names, rowsi, i) :
some row.names duplicated: 2 --> row.names NOT used
Instead of having the rownames be 1 and 2, I want them to be my original "bar" and "bar". Is there a way to over-ride xtable's duplicate replacement scheme?
Upvotes: 3
Views: 3595
Reputation: 173547
Here's the xtable solution (in a very compact form):
> print(xtable(data.frame(row = rownames(foo),data.frame(foo))),include.rownames = FALSE)
% latex table generated in R 2.14.2 by xtable 1.6-0 package
% Fri Jun 22 14:24:59 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{lrr}
\hline
row & X1 & X2 \\
\hline
bar & 0.00 & 0.00 \\
bar & 0.00 & 0.00 \\
\hline
\end{tabular}
\end{center}
\end{table}
This will still kick a warning, and it will be difficult to get your column headings right. The other option is to use latex
from the Hmisc package, which is quite a bit more flexible (but also more complicated; you can turn off the multicolumn stuff):
library(Hmisc)
> latex(foo,file = "")
% latex.default(foo, file = "")
%
\begin{table}[!tbp]
\begin{center}
\begin{tabular}{lrr}
\hline\hline
\multicolumn{1}{l}{foo}&\multicolumn{1}{c}{}&\multicolumn{1}{c}{}\tabularnewline
\hline
bar&$0$&$0$\tabularnewline
bar&$0$&$0$\tabularnewline
\hline
\end{tabular}
\end{center}
\end{table}
Sorry for the terse comment. I was holding a baby at the time.
Upvotes: 5