在Rmarkdown中如何在千位数之间添加空格?

huangapple go评论78阅读模式
英文:

How to add space between thousands in Rmarkdown?

问题

早上好,

我正在使用Rmarkdown编写文档,希望格式化所有输出表格,以便在HTML渲染中显示"10 000"而不是"10000"。

我主要使用kableExtra包。您知道是否有特殊的函数可以实现这个吗?

我尝试了以下代码:

kable(tableau_ST_CST, big.mark = " ") %>%
  kable_styling(full_width = F)

但它不起作用。

英文:

Good morning,

I am writing a document in Rmarkdown and I would like to format all the output tables so that I can have "10 000" rather than "10000" in my HTML rendering.

I work with the kableExtra package mainly. Do you know if there is a special function to do this ?

I tried this :

kable(tableau_ST_CST, big.mark = " ") %>%
kable_styling(full_width = F)

But it doesn't work.

答案1

得分: 3

你可以使用 prettyNum 函数:

library(tidyverse)
library(knitr)

tibble(nums = 10000:10010) %>%
  mutate(nums = prettyNum(nums, big.mark = " ")) %>%
  kable()
|nums   |
|:------|
|10 000 |
|10 001 |
|10 002 |
|10 003 |
|10 004 |
|10 005 |
|10 006 |
|10 007 |
|10 008 |
|10 009 |
|10 010 |

其他选项包括 formatCformat(格式相同),或者简单地使用正则表达式(str_replace_all("100000", pattern = "(\\d)(?=(\\d{3})+$)", "\\1 "))。

英文:

You can use prettyNum:

library(tidyverse)
library(knitr)

tibble(nums = 10000:10010) %>% 
  mutate(nums = prettyNum(nums, big.mark = " ")) %>%
  kable()
|nums   |
|:------|
|10 000 |
|10 001 |
|10 002 |
|10 003 |
|10 004 |
|10 005 |
|10 006 |
|10 007 |
|10 008 |
|10 009 |
|10 010 |

Other options include formatC, format (both have the same format), or simply using regex (str_replace_all("100000", pattern = "(\\d)(?=(\\d{3})+$)", "\\1 ")).

huangapple
  • 本文由 发表于 2023年7月3日 13:28:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定