如何格式化xtable输出中的列

时间:2015-06-18 22:43:58

标签: r

我有一个data.frame DF,我使用xtable创建HTML表格:

     Product    N° Clients     Capital (USD) Part. Capital (%) 
    1 A               536            2616925       33.62
    2 B               151            1613035       20.72
    3 c               112            1007983       12.95

问题在于生成html:

    DF.TAB <- xtable(DF, align = "cccrc",latex.environments="center", format.args = list(digits = 2, format = c("s","d","d","d","f"), big.mark = ","), floating = FALSE)
    print(DF.TAB,type = "html", include.rownames=FALSE, file = "DF.TAB.html")

我希望有些列(Part.Capital)有两个小数位(即使某些元素可能是整数)和逗号分隔符&#34;,&#34;并将其他列视为整数,但结果不是这样的:

     Product       N° Clients    Capital (USD) Part. Capital (%) 
    1 A               536          2616925.00       33.62
    2 B               151          1613035.00       20.72
    3 c               112          1007983.00       12.95
顺便说一下,我必须调整和格式化一个&#34;隐形&#34;专栏,因为没有它,我无法让它工作。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

这应该有效:

DF[,3]<-format(as.numeric(DF[,3]),big.mark=",")
DF.TAB <- xtable(DF, align = "cccrc",latex.environments="center", format.args = list(digits = 2, format = c("d","d","s","f")))
print(DF.TAB,type = "html", include.rownames=FALSE, file = "DF.TAB.html")

R中会产生

     Product N° Clients Capital (USD) Part. Capital (%)
[1,] "A"     "536"      "2,616,925"   "33.62"          
[2,] "B"     "151"      "1,613,035"   "20.72"          
[3,] "c"     "112"      "1,007,983"   "12.95"

HTML版本如下所示:enter image description here