[R] A better scales::dollar() ?

Fri Dec 6 12:50:46 CET 2019

I'm writing a quite large document in Rmarkdown which has financial data in it.  I format that data using scales::dollar() currently something like this:

> require (scales)
> x = 100000
> cat (dollar (x, prefix ="£", big.mark=","))


But actually, I'd quite like to get £100k out in that instance so I'd do:

> cat (dollar (x/10^3, prefix ="£", suffix="k" ))


But x could be 100 or 10,000,000.  I want some form of 'automatic' version that might give me something like:

> require (scales)
> y = 0:7
> x = 1^y
> dollar(x, prefix="£")
[1] "£1"          "£10"         "£100"        "£1,000"      "£10,000"     "£100,000"    "£1,000,000"  "£10,000,000"

But what I want is more like:

£1.00  £10.00  £100  £1k  £10k  £100k  £1m  £10m

I'm sure I can write something as a little function, but before I do - is there something already out there?

I have a similar need to format milligrams through to kilograms.


This message may contain confidential information. If you are not the intended recipient please inform the
sender that you have received the message in error before deleting it.
Please do not disclose, copy or distribute information in this e-mail or take any action in relation to its contents. To do so is strictly prohibited and may be unlawful. Thank you for your co-operation.

NHSmail is the secure email and directory service available for all NHS staff in England and Scotland. NHSmail is approved for exchanging patient data and other sensitive information with NHSmail and other accredited email services.

For more information and to find out how you can switch, https://portal.nhs.net/help/joiningnhsmail

More information about the R-help mailing list