58

I'm outputting a dataframe to html via xtable. I want to add commas to numbers in a couple columns of the table. I figured before I did my own paste hack I'd check if there is a built in way to do this.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Dan
  • 6,008
  • 7
  • 40
  • 41

5 Answers5

81

You might want to consider transforming the column using formatC

> formatC(1:10 * 100000, format="d", big.mark=",")
 [1] "100,000"   "200,000"   "300,000"   "400,000"   "500,000"   "600,000"  
 [7] "700,000"   "800,000"   "900,000"   "1,000,000"
Max Ghenis
  • 14,783
  • 16
  • 84
  • 132
Jonathan Chang
  • 24,567
  • 5
  • 34
  • 33
17

Huge thanks to Jonathan Chang for his answer. formatC looks to be an extremely useful function. This inspired me to read the documentation for it, wherein I found prettyNum, which turned out to be a pretty elegant solution to a similar problem I was having. Here's a minimum viable example of what I did to add commas to numbers in a data frame named enrollment.summary.

xtable(prettyNum(enrollment.summary,big.mark=","))

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
woodrad
  • 179
  • 1
  • 2
  • 1
    `formatC` is a [function](https://stat.ethz.ch/R-manual/R-devel/library/base/html/formatc.html) in the `base` package, not a package itself. In the future, please include reproducible examples and display the behavior in your answer. For example, consider replacing `enrollment.summary` with `mtcars`: `prettyNum(mtcars, big.mark=",")` returns a character matrix where columns correspond to columns in the `mtcars` data.frame, and numbers are printed. – Max Ghenis Mar 29 '15 at 19:20
  • 3
    Also note that `prettyNum` pads your output with (potentially unwanted) blank space: prettyNum(c(123,1234),big.mark=","); gives " 123" "1,234". add preserve.width="none" to prevent this. – MichaelChirico May 21 '15 at 19:44
14

You can also try using the function argument 'format.args'

  ## Demonstration of additional formatC() arguments.
  print(fm1.table, format.args = list(big.mark = "'", decimal.mark = ","))

from here

https://cran.rstudio.com/web/packages/xtable/xtable.pdf

steveb
  • 5,382
  • 2
  • 27
  • 36
user3357059
  • 1,122
  • 1
  • 15
  • 30
13

Here is a late answer, but you could also use scales::comma_format as follows:

library(scales)
values <- c(1000000.789, 8888.23)
comma_format(digits = 12)(values)
## [1] "1,000,000.789" "8,888.230"

For just integer values, you can just use comma:

library(scales)
int_vals <- c(1234, 5678)
comma(int_vals)
## [1] "1,234" "5,678"
steveb
  • 5,382
  • 2
  • 27
  • 36
  • One thing which goes wrong for me is that it converts it to string – Sovik Gupta Jul 07 '20 at 06:38
  • @SovikGupta Yes, it will. You only want to add the commas when you display it. If you have the data in a `data.frame` you would likely keep it as numeric. What is your circumstance where this is causing an issue ? – steveb Jul 07 '20 at 06:52
  • @SovikGupta An example using `dplyr` could either add a string version of the column as in `dplyr::tibble(A = c(1:5) * 1e6) %>% dplyr::mutate(A.as.String = scales::comma(A))` or just output as a string w/o modifying the data frame as in `dplyr::tibble(A = c(1:5) * 1e6) %>% dplyr::mutate(A = scales::comma(A))` – steveb Jul 07 '20 at 07:02
7

to format some summaries from dplyr, here is boilerplate code:

df %>%
    summarise(mu=mean(big_values),
              min=min(big_values),
              max=max(big_values)) %>%
    mutate_each(funs(prettyNum(., big.mark=",")))
Paul
  • 3,920
  • 31
  • 29