114

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

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

Michael Currie
  • 13,721
  • 9
  • 42
  • 58
TekTimmy
  • 3,066
  • 2
  • 29
  • 33

4 Answers4

158

You can try either format or prettyNum, but both functions return a vector of characters. I'd only use that for printing.

> prettyNum(12345.678,big.mark=",",scientific=FALSE)
[1] "12,345.68"
> format(12345.678,big.mark=",",scientific=FALSE)
[1] "12,345.68"

EDIT: As Michael Chirico says in the comment:

Be aware that these have the side effect of padding the printed strings with blank space, for example:

> prettyNum(c(123,1234),big.mark=",")
[1] "  123" "1,234"

Add trim=TRUE to format or preserve.width="none" to prettyNum to prevent this:

> prettyNum(c(123,1234),big.mark=",", preserve.width="none")
[1] "123"   "1,234"
> format(c(123,1234),big.mark=",", trim=TRUE)
[1] "123"   "1,234"
Joris Meys
  • 106,551
  • 31
  • 221
  • 263
  • 6
    Be aware that these have the side effect of padding the printed strings with blank space, for example: `prettyNum(c(123,1234),big.mark=",")`; gives `" 123" "1,234"`. add `trim=T` to `format` or `preserve.width="none"` to `prettyNum` to prevent this. – MichaelChirico May 21 '15 at 19:41
  • @MichaelChirico Thanks for the extra information. I've added it to the answer. – Joris Meys May 22 '15 at 08:37
  • Note if you are also settings digits for decimal places, if you have a vector this can go awry if the numebrs are quite different. From help "Numeric vectors are encoded with the minimum number of decimal places needed to display all the elements to at least the digits significant digits. However, if all the elements then have trailing zeroes, the number of decimal places is reduced until nsmall" – micstr Dec 10 '15 at 11:36
  • 1
    So `format(c(12345678,0.001234),big.mark=",", trim=TRUE, digits = 2, scientific = FALSE)` gives ` "12,345,678.0000" "0.0012" so enough can be seen. i.e. the places formatting is not done on an element by element basis. Caught me out so wanted to share this. – micstr Dec 10 '15 at 11:44
  • Spent quite some time trying to find `scientific` which is hidden in the haystack of arguments to `format`. Thanks! – MichaelChirico Feb 26 '16 at 04:21
  • This doesn't really work for large numbers. `prettyNum(123123123.45, big.mark=",", scientific=FALSE)` returns `123,123,123` rather than `123,123,123.45` – airstrike Feb 28 '16 at 15:06
  • @AndréTerra It does. prettyNum(123123123.45, big.mark=",", scientific=FALSE,digits = 11). Reading `?prettyNum` would have told you that the default number of digits for a real, is 4, but the field width if not set, is digits + 1. Obviously that's not enough to have the decimal part displaying, so you have to set that one manually. – Joris Meys Feb 28 '16 at 16:24
  • This worked for me when I added the above options, plus the `drop0trailing = TRUE` option to remove trailing zeroes that would otherwise be added given a mix of data types that had both integer values and decimals. – Dave Jan 21 '20 at 23:21
35

See ?format:

> format(1e6, big.mark=",", scientific=FALSE)
[1] "1,000,000" 
>   
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
14

The other answers posted obviously work - but I have always used

library(scales)    
label_comma()(1000000)
Rico
  • 1,998
  • 3
  • 24
  • 46
MatthewR
  • 2,660
  • 5
  • 26
  • 37
  • The function comma_format can only deal with integers. – Ven Yao Oct 18 '15 at 08:21
  • 3
    This is a very late response, and it may have been true in the past that `comma_format` didn't handle real numbers but now you can do the following: `scales::comma_format(digits = 12)(1000000.789)` which results in the following: `"1,000,000.789"`. – steveb Mar 20 '17 at 18:50
  • 3
    scales::comma(1000000) seems worth a mention. – Joe Sep 26 '18 at 17:48
10

I think Joe's comment to MatthewR offers the best answer and should be highlighted:

As of Sept 2018, the scales package (part of the Tidyverse) does exactly this:

> library(scales)

> x <- 10e5
> comma(x)

[1] "1,000,000"

The scales package appears to play very nicely with ggplot2, allowing for fine control of how numerics are displayed in plots and charts.

Arthur Small
  • 621
  • 1
  • 6
  • 15
  • 1
    I upvoted this but then when I looked at `comma()` I saw it's retired in favor of `label_comma` which returns a function rather than a string. I'll go back to preferring `prettyNum`, even though `big.mark=","` is not a default argument, so I still need to pass that explicitly each time. – Dannid Jul 26 '21 at 18:21
  • @Dannid Did you try the function as follows? `scales::label_comma()(1000000)` I believe it works fine. – Rico Sep 30 '21 at 16:10