3

给定以下示例:

library(pander)
tableAbs <- Titanic[1, 1, , ]
tablePct <- round(prop.table(tableAbs) * 100, 2)
table <- cbind(tableAbs, tablePct)
pander(table)

----------------------------------
  &nbsp;     No   Yes   No    Yes 
----------- ---- ----- ----- -----
 **Child**   0     5     0   2.78 

 **Adult**  118   57   65.56 31.67
----------------------------------

我想在那个0百分比上保留所有尾随零,所以这就是我所做的:

panderOptions("keep.trailing.zeros", TRUE)
pander(table)

------------------------------------
  &nbsp;      No    Yes   No    Yes 
----------- ------ ----- ----- -----
 **Child**   0.00  5.00  0.00  2.78 

 **Adult**  118.00 57.00 65.56 31.67
------------------------------------

现在的问题是,即使是绝对频率也.00附在它们上面。由于这些是自然数,因此保留这些尾随零毫无意义。我该怎么做呢?

4

1 回答 1

2

感谢评论中的rawr。

您可以使用format保留尾随零。这会将四舍五入的值转换为,character同时保留表格的尺寸。

tablePct <- format(round(prop.table(tableAbs) * 100, 2))

编辑

似乎在xtabs课堂上工作正常

mtcars$am[mtcars$vs == 1]  <- 0
x <- xtabs(~ am + vs, data=mtcars)
tab <- format(round(100*prop.table(x), 2))
tab <- cbind(x, tab)
pander(tab)

---------------------------
&nbsp;   0   1    0     1  
------- --- --- ----- -----
 **0**  12  14  37.50 43.75

 **1**   6   0  18.75 0.00 
---------------------------
于 2015-05-29T21:31:42.077 回答