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

Hadley Wickham h@w|ckh@m @end|ng |rom gm@||@com
Mon Dec 9 16:27:36 CET 2019


You can get pretty close with label_number_si():

pounds <- scales::label_number_si(prefix = "£")
pounds(10 ^ (0:7))
#> [1] "£1"    "£10"   "£100"  "£1K"   "£10K"  "£100K" "£1M"   "£10M"

<sup>Created on 2019-12-09 by the [reprex
package](https://reprex.tidyverse.org) (v0.3.0.9001)</sup>

Hadley

On Fri, Dec 6, 2019 at 5:51 AM POLWART, Calum (COUNTY DURHAM AND
DARLINGTON NHS FOUNDATION TRUST) via R-help <r-help using r-project.org>
wrote:
>
> 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=","))
>
> £100,000
>
> 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" ))
>
> £100k
>
> 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 ...{{dropped:27}}



More information about the R-help mailing list