在数字中添加逗号以进行输出

发布于 2024-08-07 20:33:33 字数 82 浏览 6 评论 0原文

我通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

々眼睛长脚气 2024-08-14 20:33:33

您可能需要考虑使用 formatC 转换列

> formatC(1:10 * 100000, format="d", big.mark=",")
 [1] "100,000"   "200,000"   "300,000"   "400,000"   "500,000"   "600,000"  
 [7] "700,000"   "800,000"   "900,000"   "1,000,000"

You might want to consider transforming the column using formatC

> formatC(1:10 * 100000, format="d", big.mark=",")
 [1] "100,000"   "200,000"   "300,000"   "400,000"   "500,000"   "600,000"  
 [7] "700,000"   "800,000"   "900,000"   "1,000,000"
终难愈 2024-08-14 20:33:33

非常感谢 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 found prettyNum, 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 named enrollment.summary.

xtable(prettyNum(enrollment.summary,big.mark=","))

唯憾梦倾城 2024-08-14 20:33:33

函数参数 'format.args'

  ## Demonstration of additional formatC() arguments.
  print(fm1.table, format.args = list(big.mark = "'", decimal.mark = ","))

您还可以尝试使用此处的

https:// /cran.rstudio.com/web/packages/xtable/xtable.pdf

You can also try using the function argument 'format.args'

  ## Demonstration of additional formatC() arguments.
  print(fm1.table, format.args = list(big.mark = "'", decimal.mark = ","))

from here

https://cran.rstudio.com/web/packages/xtable/xtable.pdf

场罚期间 2024-08-14 20:33:33

这是一个较晚的答案,但您也可以使用 scales::comma_format 如下:

library(scales)
values <- c(1000000.789, 8888.23)
comma_format(digits = 12)(values)
## [1] "1,000,000.789" "8,888.230"

对于整数值,您可以只使用逗号:

library(scales)
int_vals <- c(1234, 5678)
comma(int_vals)
## [1] "1,234" "5,678"

Here is a late answer, but you could also use scales::comma_format as follows:

library(scales)
values <- c(1000000.789, 8888.23)
comma_format(digits = 12)(values)
## [1] "1,000,000.789" "8,888.230"

For just integer values, you can just use comma:

library(scales)
int_vals <- c(1234, 5678)
comma(int_vals)
## [1] "1,234" "5,678"
白云不回头 2024-08-14 20:33:33

要格式化来自 dplyr 的一些摘要,这里是样板代码:

df %>%
    summarise(mu=mean(big_values),
              min=min(big_values),
              max=max(big_values)) %>%
    mutate_each(funs(prettyNum(., big.mark=",")))

to format some summaries from dplyr, here is boilerplate code:

df %>%
    summarise(mu=mean(big_values),
              min=min(big_values),
              max=max(big_values)) %>%
    mutate_each(funs(prettyNum(., big.mark=",")))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文