在数字中添加逗号以进行输出
我通过 xtable 将数据帧输出到 html。我想在表格的几列中的数字中添加逗号。我想在我自己做粘贴黑客之前我会检查是否有内置的方法可以做到这一点。
I'm outputting a dataframe to html via xtable
. I want to add commas to numbers in a couple columns of the table. I figured before I did my own paste hack I'd check if there is a built in way to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能需要考虑使用
formatC
转换列You might want to consider transforming the column using
formatC
非常感谢 Jonathan Chang 的回答。
formatC
看起来是一个非常有用的函数。这启发我阅读了它的文档,其中我发现了prettyNum
,结果证明这是一个非常优雅的解决方案,可以解决我遇到的类似问题。下面是一个最小可行的示例,展示了我在名为enrollment.summary
的数据框中向数字添加逗号的操作。xtable(prettyNum(enrollment.summary,big.mark=","))
Huge thanks to Jonathan Chang for his answer.
formatC
looks to be an extremely useful function. This inspired me to read the documentation for it, wherein I foundprettyNum
, which turned out to be a pretty elegant solution to a similar problem I was having. Here's a minimum viable example of what I did to add commas to numbers in a data frame namedenrollment.summary
.xtable(prettyNum(enrollment.summary,big.mark=","))
函数参数 'format.args'
您还可以尝试使用此处的
https:// /cran.rstudio.com/web/packages/xtable/xtable.pdf
You can also try using the function argument 'format.args'
from here
https://cran.rstudio.com/web/packages/xtable/xtable.pdf
这是一个较晚的答案,但您也可以使用
scales::comma_format
如下:对于整数值,您可以只使用逗号:
Here is a late answer, but you could also use
scales::comma_format
as follows:For just integer values, you can just use comma:
要格式化来自 dplyr 的一些摘要,这里是样板代码:
to format some summaries from
dplyr
, here is boilerplate code: