[R] Bug/Error in formatC? (Was: Why doesn't formatC( x, digits=2, format= "g")...)

From: Peter Dunn <dunn_at_usq.edu.au>
Date: Tue, 10 Jun 2008 09:25:07 +1000

Hi all

After posting what follows, Duncan Murdoch suggested perhaps a bug in formatC, or an error on documentation. Any comments?

In particular, bug, error or not, any ideas about how I can consistently get two significant figures to print?


Hi all

I am not a C programmer, but I am trying to understand formatC to get consistent printing of reals to a given number of significant digits.

Can someone please explain this to me? These first three give what I expect on reading ?formatC:

> formatC(0.0059999, digits=2,format="fg",flag="#")
[1] "0.0060"
> formatC(0.59999, digits=2,format="fg",flag="#")
[1] "0.60"
> formatC(5.9999, digits=2,format="fg",flag="#")
[1] "6.0"

This seems consistent with what I read (but perhaps do not understand) in ?formatC, where I read this:

        digits   the desired number of digits after the decimal
 	point (format = "f") or significant digits (format = "g", 
	= "e" or = "fg"). 

Since I am using format="fg" and digits=2, so I am expecting two significant digits to always show, which I have above. So I fail to understand this:

> formatC(0.000059999, digits=2,format="fg",flag="#")
[1] "0.00006"
> formatC(0.000059, digits=2, format="fg",flag="#")
[1] "0.000059"

I was expecting both of these to produce "0.000059". But in the first case above, I get one significant digit only.

I'm obviously misunderstanding something; can someone enlighten me? (No doubt, someone will point out a nuance of the help files I didn't understand!)

Also, since the above obviously doesn't do what I hoped (consistently printing two sig figs), could someone also explain how I can consistently get two significant figures in situation like above?

Thanks as always.


Dr Peter Dunn  |  dunn <at> usq.edu.au
Faculty of Sciences, USQ; http://www.sci.usq.edu.au/staff/dunn
Aust. Centre for Sustainable Catchments: www.usq.edu.au/acsc

This email (including any attached files) is confidentia...{{dropped:15}}

R-help_at_r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Mon 09 Jun 2008 - 23:36:15 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 10 Jun 2008 - 13:30:43 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive