How to format numbers with commas in R
· 1 min read · Updated March 14, 2026 · beginner
r formatting strings display
Adding commas as thousand separators makes large numbers easier to read.
With base R
Use format() with big.mark = ",":
x <- c(1000, 1234567.89, 999999)
format(x, big.mark = ",")
# [1] "1,000" "1,234,567.89" "999,999"
# For currency-like formatting
format(x, big.mark = ",", decimal.mark = ".", nsmall = 2)
# [1] "1,000.00" "1,234,567.89" "999,999.00"
With scales
The scales package provides convenient wrappers:
library(scales)
x <- c(1000, 1234567.89, 999999)
comma(x)
# [1] "1,000" "1,234,567.89" "999,999"
dollar(x) # Adds $ prefix
# [1] "$1,000" "$1,234,567.89" "$999,999"
percent(0.123) # Formats as percentage
# [1] "12.3%"
With dplyr and ggplot2
Apply formatting directly in pipelines:
library(dplyr)
df <- data.frame(
product = c("Widget A", "Widget B", "Widget C"),
sales = c(1500, 2345678, 99999)
)
df %>%
mutate(sales_formatted = comma(sales))
With formattable
For prettier tables:
library(formattable)
x <- account(c(1000, 1234567.89, -500))
format(x, digits = 2)
# [1] "1,000.00" "1,234,567.89" "-500.00"
currency(x)
# [1] "$1,000.00" "$1,234,567.89" "$-500.00"
In ggplot2 labels
library(ggplot2)
ggplot(df, aes(x = product, y = sales)) +
geom_bar(stat = "identity") +
scale_y_continuous(labels = comma)