[Rd] Commas in formatC
Jonathan Rougier
J.C.Rougier@durham.ac.uk
Fri, 22 Mar 2002 08:13:57 +0000
Martin Maechler wrote:
>
> >>>>> "DavidB" == David Brahm <brahm@alum.mit.edu> writes:
>
> DavidB> formatC() is great for formatting numbers! But it
> DavidB> would be even better if it could optionally insert
> DavidB> commas (or semicolons), e.g.
>
> R> formatC(1234567.89, digits=2, format="f", commas=T)
> DavidB> [1] "1,234,567.89"
>
> DavidB> Here's a snippet of code that does that, which could
> DavidB> more or less just be inserted into at the end of
> DavidB> formatC if any R-core guru were so inclined. "r" is
> DavidB> the result so far, and "commas" is a logical flag:
>
> DavidB> stp <- regexpr("[0-9]+", r); len <- attr(stp, "match.length")
> DavidB> if (commas && (maxl <- max(len)) > 3) for (i in seq(3, maxl-1, 3))
> DavidB> r[len>i] <- paste(substring(r[len>i], 1, (stp+len-1-i)[len>i]),
> DavidB> substring(r[len>i], (stp+len-i)[len>i]), sep=",")
>
> Thank you, David, for the suggestion.
>
> I'm looking at adding something quite a bit more general
> to both format.default() and formatC(), using a new function
> prettyNum() and using new arguments (that are compatible to S-plus'
> format.default).
Hi Martin,
Maybe this is a step in the wrong direction, but I actually like using C
string formatting, eg I would like to be able to type something like
str <- stringC("The man called %s is %i feet tall", "Sven", 6)
which is also used in Python via the string formatting operator, '%'.
Is this a possibility?
Cheers, Jonathan.
--
Jonathan Rougier Science Laboratories
Department of Mathematical Sciences South Road
University of Durham Durham DH1 3LE
tel: +44 (0)191 374 2361, fax: +44 (0)191 374 7388
http://www.maths.dur.ac.uk/stats/people/jcr/jcr.html
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._