英文:
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 |
其他选项包括 formatC
、format
(格式相同),或者简单地使用正则表达式(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 ")
).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论