[Rd] Commas in formatC
David Brahm
brahm@alum.mit.edu
Wed, 13 Mar 2002 13:05:14 -0500
formatC() is great for formatting numbers! But it would be even better if it
could optionally insert commas (or semicolons), e.g.
R> formatC(1234567.89, digits=2, format="f", commas=T)
[1] "1,234,567.89"
Here's a snippet of code that does that, which could more or less just be
inserted into at the end of formatC if any R-core guru were so inclined. "r"
is the result so far, and "commas" is a logical flag:
stp <- regexpr("[0-9]+", r); len <- attr(stp, "match.length")
if (commas && (maxl <- max(len)) > 3) for (i in seq(3, maxl-1, 3))
r[len>i] <- paste(substring(r[len>i], 1, (stp+len-1-i)[len>i]),
substring(r[len>i], (stp+len-i)[len>i]), sep=",")
--
-- David Brahm (brahm@alum.mit.edu)
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._