POLWART, Calum (COUNTY DURHAM AND DARLINGTON NHS FOUNDATION TRUST)
2019-Dec-06 11:50 UTC
[R] A better scales::dollar() ?
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 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
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 at 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}}