3

我希望 xtable 不会覆盖我重复的行名条目。考虑以下 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

我希望它们成为我原来的“bar”和“bar”,而不是让 rownames 成为 1 和 2。有没有办法覆盖 xtable 的重复替换方案?

4

1 回答 1

5

这是xtable解决方案(以非常紧凑的形式):

> 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}

这仍然会发出警告,并且很难让您的列标题正确。另一种选择是使用latexHmisc,它更灵活(但也更复杂;你可以关闭多列的东西):

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}

对不起,简短的评论。当时我正抱着一个婴儿。

于 2012-06-22T21:27:18.523 回答