有可以使用两种基本的选择:转换输出到一个行矩阵使用xtable
之前,或者您可以使用apply
转换为prettyNum
。
这里是一个简单的例子:
首先,一些样本数据。 A中的一行data.frame
和三排data.frame
:在3行
df <- structure(
list(V1 = 76491283764.9743, V2 = 29.12345678901, V3 = -7.1234, V4 = -100.1,
V5 = 1123), .Names = c("V1", "V2", "V3", "V4", "V5"),
row.names = c(NA, -1L), class = "data.frame")
df
# V1 V2 V3 V4 V5
# 1 76491283765 29.12346 -7.1234 -100.1 1123
df2 <- rbind(df, df*2, df*3)
df2
# V1 V2 V3 V4 V5
# 1 76491283765 29.12346 -7.1234 -100.1 1123
# 2 152982567530 58.24691 -14.2468 -200.2 2246
# 3 229473851295 87.37037 -21.3702 -300.3 3369
使用xtable
一个正常工作,但不像你在你的问题说,它不保留data.frame
。它将输出转换为字符矩阵。:
> class(prettyNum(df2, big.mark=","))
[1] "matrix"
> xtable(prettyNum(df2, big.mark=","), align = "rrrrrr")
% latex table generated in R 2.15.3 by xtable 1.7-1 package
% Fri Mar 22 15:46:08 2013
\begin{table}[ht]
\centering
\begin{tabular}{rrrrrr}
\hline
& V1 & V2 & V3 & V4 & V5 \\
\hline
1 & 76,491,283,765 & 29.12346 & -7.1234 & -100.1 & 1,123 \\
2 & 152,982,567,530 & 58.24691 & -14.2468 & -200.2 & 2,246 \\
3 & 229,473,851,295 & 87.37037 & -21.3702 & -300.3 & 3,369 \\
\hline
\end{tabular}
\end{table}
尝试相同的1行data.frame给了我们一个错误,因为在“美化”你的号码的过程中,data.frame成为指定的字符向量,因为你已经想通了:
> xtable(prettyNum(df, big.mark = ","), align = "rrrrrr")
Error in UseMethod("xtable") :
no applicable method for 'xtable' applied to an object of class "character"
使用methods(xtable)
看到xtable
方法已经为其定义对象的类型。要查看特定方法 - 例如,xtable.data.frame
方法 - 请使用xtable:::xtable.data.frame
。从那里,你可以学习如何编写自己的方法,如果你认为这是必要的。
但是,在这种情况下,没有必要。刚刚尝试下列选项之一,并指出它们之间的细微差别:
xtable(matrix(prettyNum(df, big.mark = ","), nrow = 1), align = "rrrrrr")
xtable(t(apply(df, 1, prettyNum, big.mark = ",")), align = "rrrrrr")
下面是一些示例输出:
逾期感谢您一个全面的答案阿难。 – Rickard