本文介绍了仅保留百分比的尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出以下示例:
library(pander)
tableAbs <- Titanic[1, 1, , ]
tablePct <- round(prop.table(tableAbs) * 100, 2)
table <- cbind(tableAbs, tablePct)
pander(table)
----------------------------------
No Yes No Yes
----------- ---- ----- ----- -----
**Child** 0 5 0 2.78
**Adult** 118 57 65.56 31.67
----------------------------------
我想在那个 0
百分比上保留所有尾随零,所以这就是我要做的:
I would like to keep all trailing zeros on that 0
percentage over there, so this is what I do:
panderOptions("keep.trailing.zeros", TRUE)
pander(table)
------------------------------------
No Yes No Yes
----------- ------ ----- ----- -----
**Child** 0.00 5.00 0.00 2.78
**Adult** 118.00 57.00 65.56 31.67
------------------------------------
现在的问题是,即使是绝对频率也附有 .00
.由于这些是自然数,因此保留那些尾随零是没有意义的.我该怎么做?
The problem is now that even the absolute frequencies have .00
attached to them. Since those are natural numbers, it makes little sense to keep those trailing zeros. How do I do this?
推荐答案
感谢评论中的 rawr.
Thanks to rawr in the comments.
您可以使用 format
来保留尾随零.这会将四舍五入的值转换为 character
,同时保留表格的维度.
You can use format
to keep the trailing zeros. This converts the rounded values to character
while preserving the dimensions of the table.
tablePct <- format(round(prop.table(tableAbs) * 100, 2))
编辑
似乎可以与 xtabs
类配合使用
Seems to work okay with xtabs
class
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)
---------------------------
0 1 0 1
------- --- --- ----- -----
**0** 12 14 37.50 43.75
**1** 6 0 18.75 0.00
---------------------------
这篇关于仅保留百分比的尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!