有趣的问题。我也找不到使用 xtable 本身来处理这个问题的方法。所以我能建议的最好的方法是将你的解决方法变成一个可以轻松调用的小函数。
例如:
# Construct some data
df <- data.frame(
x1 = addNA(sample(c(NA, LETTERS[1:4]), 100, replace = TRUE)),
x2 = addNA(sample(c(NA, letters[24:26]), 100, replace = TRUE))
)
# Create a function to rename NA row and column names in a data.frame
rename_NA <- function(x){
rownames(x)[is.na(rownames(x))] <- "NA"
colnames(x)[is.na(colnames(x))] <- "NA"
x
}
tab <- rename_NA(xtabs(~x1+x2, data=df))
xtable(tab)
这将创建没有错误的有效乳胶:
% latex table generated in R 2.13.0 by xtable 1.5-6 package
% Wed Apr 27 17:20:21 2011
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrrr}
\hline
& x & y & z & NA \\
\hline
A & 4.00 & 7.00 & 10.00 & 4.00 \\
B & 6.00 & 5.00 & 4.00 & 2.00 \\
C & 8.00 & 4.00 & 4.00 & 2.00 \\
D & 8.00 & 5.00 & 1.00 & 6.00 \\
NA & 5.00 & 2.00 & 7.00 & 6.00 \\
\hline
\end{tabular}
\end{center}
\end{table}