I'm trying to format the numbers in graphs and tables, but there are a lot of them. Therefore I am wondering if there is a function or possibility to set a default number of decimals and a default thousand separator for the entire script?
Asked
Active
Viewed 154 times
1
-
Does R even do thousands-separators? I'd think only table-rendering functions (`knitr`, `formattable`, `kableExtra`, etc) would provide that functionality. For that option, look into whatever package you're using for table rendering and figure out how it's doing it. (Note: thousands-separators is not a `numeric` thing in R, it is only a *rendering* thing, which means you need to be a bit explicit about doing it. Once you add commas, R will only think of it as a string.) – r2evans Mar 05 '21 at 18:19
1 Answers
0
you can use comma
from formattable
package.
library(formattable)
comma(5000.907, digits = 2)
# [1] 5,000.91

TarJae
- 72,363
- 6
- 19
- 66
-
1Please note the _for an entire script_ part of the question. Otherwise, it would be a dupe of [Format number in R with both comma thousands separator and specified decimals](https://stackoverflow.com/questions/29465941/format-number-in-r-with-both-comma-thousands-separator-and-specified-decimals), which also includes your suggestion. – Henrik Mar 05 '21 at 18:50
-
-
If the graphs and tables have a common structure, you could write a function to do the figure/table generation that includes the formatting. – Rajesh S Mar 06 '21 at 12:46
-
1Thanks a lot! I'm gonna try writing a function to help me with the task! – Maarten van Riel Mar 15 '21 at 18:32