[R] comma separated format
David Winsemius
dwinsemius at comcast.net
Fri Nov 12 00:44:54 CET 2010
On Nov 11, 2010, at 5:51 PM, sachinthaka.abeywardana at allianz.com.au
wrote:
>
> Hi All,
>
> I'm trying to create labels to plot such that it doesn't show up as
> scientific notation. So for example how do I get R to show 1e06 as
> $1,000,000.
Knowing how these things usually have even more compact solutions than
I usually uncover at first, I will not be at all surprised if there
are improvements offered:
fc <- formatC(1.234 * 10^(0:8), format="fg", big.mark = ",")
sprintf("$%s", fc)
#
#[1] "$1.234" "$12.34" "$123.4" "$1,234"
"$12,340" "$123,400"
# "$1,234,000"
# [8] "$12,340,000" "$123,400,000"
So the vectorized one liner would be
> commaUS <- function(x) sprintf("$%s", formatC(x, format="fg",
big.mark = ","))
> commaUS(c(1e06, 1e07, 1e08))
[1] "$1,000,000" "$10,000,000" "$100,000,000"
I could not find the right specs to formatC to get the "$" in the
right place.
I see ggplot2 has a dollar() function from which a big.mark argument
will be passed through to format(), but this suggests to me that there
might not be a simpler solution in base R. The rrv package has a
money() function.
--
David.
>
> I was wondering if there was a single function which allows you to
> do that,
> the same way that as.Date() allows you to show in date format on a
> plot.
>
> Thanks,
> Sachin
>
> --- Please consider the environment before printing this email ---
>
> Allianz - Best General Insurance Company of the Year 2010*
> Allianz - General Insurance Company of the Year 2009+
>
> * Australian Banking and Finance Insurance Awards
> + Australia and New Zealand Insurance Industry Awards
>
> This email and any attachments has been sent by Allianz Australia
> Insurance Limited (ABN 15 000 122 850) and is intended solely for
> the addressee. It is confidential, may contain personal information
> and may be subject to legal professional privilege. Unauthorised use
> is strictly prohibited and may be unlawful. If you have received
> this by mistake, confidentiality and any legal privilege are not
> waived or lost and we ask that you contact the sender and delete and
> destroy this and any other copies. In relation to any legal use you
> may make of the contents of this email, you must ensure that you
> comply with the Privacy Act (Cth) 1988 and you should note that the
> contents may be subject to copyright and therefore may not be
> reproduced, communicated or adapted without the express consent of
> the owner of the copyright.
> Allianz will not be liable in connection with any data corruption,
> interruption, delay, computer virus or unauthorised access or
> amendment to the contents of this email. If this email is a
> commercial electronic message and you would prefer not to receive
> further commercial electronic messages from Allianz, please forward
> a copy of this email to unsubscribe at allianz.com.au with the word
> unsubscribe in the subject header.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list