【问题标题】:Format numbers in R在 R 中格式化数字
【发布时间】:2020-01-31 02:10:19
【问题描述】:

我有一个带有数字的数据框。我需要通过添加逗号来格式化它。

df
ColA   ColB
sd     68900744.0
fg     78900744.0
gh     88900744.0
fg     1375292.0

预期输出

df
ColA   ColB
sd     68,900,744
fg     78,900,744
gh     88,900,744
fg     1,375,292

【问题讨论】:

标签: r


【解决方案1】:

欲了解更多信息,请参阅?formatC

  df$ColB_new <- formatC(df$ColB, format="f", big.mark=",", digits=0)

有关其他选项,请参阅 Format number in R with both comma thousands separator and specified decimalsComma separator for numbers in R?

【讨论】:

  • 谢谢。但它正在将数字转换为字符?不能只保留数字吗?
  • 你需要它做什么?
  • 我也需要总结一下。而且我还需要用逗号显示
  • 您可以进行计算,然后将其更改为最终输出。也许尝试自定义打印功能:my_print &lt;- function(dta){dta$ColB &lt;- formatC(...); print(dta)}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多