[Rd] Commas in formatC
Martin Maechler
Martin Maechler <maechler@stat.math.ethz.ch>
Thu, 21 Mar 2002 17:25:35 +0100
>>>>> "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).
Martin Maechler <maechler@stat.math.ethz.ch> http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum LEO C16 Leonhardstr. 27
ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND
phone: x-41-1-632-3408 fax: ...-1228 <><
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._