Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)
edited by

Is there a function in R to display large numbers separated with commas?

i.e., from 1000000 to 1,000,000.

1 Answer

0 votes
by
edited by

You can use the format or prettyNum function to represent numbers with commas as follows:

format(13579.246,big.mark=",",scientific=FALSE)

[1] "13,579.25"

prettyNum(13579.246,big.mark=",",scientific=FALSE)

[1] "13,579.25"

To avoid the padding of printed strings with blank, use the following:

format(c(123,1234),big.mark=",", ) #with padding

[1] "  123" "1,234"

> format(c(123,1234),big.mark=",", trim=TRUE) #without padding

[1] "123"   "1,234"

Similarly in prettyNum

prettyNum(c(123,1234),big.mark=",", preserve.width="none")

[1] "123"   "1,234"

...