1

I have a dataframe in R that contains a column called AverageSalary with just numbers that vary in length from 1-4 integers. I want to add 3 0's to the end of the number regardless of length and add a comma for every 3 integers.

I have tried a concatenation and sprintf (gives me leading zeros). Does anybody have any ideas?

Average Salary Desired Format
             3          3,000
           144        144,000
          5678      5,678,000
M--
  • 25,431
  • 8
  • 61
  • 93
Arsenal
  • 13
  • 2

2 Answers2

0

Using formatC.

x <- c(3, 144, 5678)

formatC(x*1e3, format='f', digits=0, big.mark=',')
# [1] "3,000"     "144,000"   "5,678,000"
jay.sf
  • 60,139
  • 8
  • 53
  • 110
0

You could also use scales::comma():

library(scales)

comma(AverageSalary * 1000)
# "3,000"     "144,000"   "5,678,000"
zephryl
  • 14,633
  • 3
  • 11
  • 30