我想获取一个百分比表,将值格式化为百分比并以良好的格式显示它们。如果重要的话,我正在使用 RStudio 并编织为 PDF。
我看过其他关于此的帖子,但它们看起来都不干净,而且效果不佳。
例如,下面的 apply 语句确实采用百分比格式,但是,它去掉了年份,并用引号将表格括起来。我可以使用 kable 修复引号,但这似乎是一个很常见的问题,有一个包或技巧可以解决这个问题。
任何帮助将不胜感激。
myTable <- structure(c(.20, .10, .25, .10,
.20, .10, .25, .20,
.20, .10, .25, .30,
.20, .10, .25, .40,
.20, .60, .25, .0),
class = "table",
.Dim = c(5L, 4L),
.Dimnames = structure(list(c("2006", "2007", "2008", "2009", "2010"),
c("1", "2", "3", "4")),
.Names = c("", "")))
# Table is fine, but needs decimal shifted and % sign added
myTable
formattedTable <- apply(myTable*100, 2, function(u) sprintf("%.0f%%", u))
# Decimal is shifted and % sign added, but no years, and quotes around text
formattedTable
# Fixes the quotes issue, still no years
kable(formattedTable)