[R] Numbers with correct significant digits

RMan54 RMan54 at cox.net
Sat Nov 18 00:14:31 CET 2006


Yes, you can always tweak one number but it may mess up the others. That's
why my example included two numbers. When you use sprintf this way:

v  <- c(9.6996, 99.99)
sprintf("%.2f", v)

you get:

"9.70"  "99.99"

Now I have 4 significant digits for the 2nd number! The ".2" in the format
string always gives you 2 digits after the decimal point but not always 3
significant digits.

Rene


Andrew Robinson-6 wrote:
> 
> I have found that sprintf gets this right, although the syntax of the
> command itself is a little less clear.
> 
>> sprintf("%.2f", 9.6996)
> [1] "9.70" 
> 
> I hope that this helps,
> 
> Andrew
> 
> On Fri, Nov 17, 2006 at 02:14:58PM -0800, RMan54 wrote:
>> 
>> This, for example:
>> 
>> v  <- c(9.6996, 99.99)
>> formatC(v, digits=3, format="g")
>> 
>> shows:
>> 
>> " 9.7" " 100"
>> 
>> This is scientifically incorrect for the first number in the sense that I
>> like to show all 3 significant digits, including trailing zero's.
>> Is there a way that the first number would show as " 9.70"?
>> 
>> By the way, can't use format() since it applies the same numbers of
>> digits
>> after the decimal point for all numbers in the vector.
>> 
>> Thanks,
>> Rene
>> 
>> -- 
>> View this message in context:
>> http://www.nabble.com/Numbers-with-correct-significant-digits-tf2657246.html#a7412042
>> Sent from the R help mailing list archive at Nabble.com.
>> 
>> ______________________________________________
>> R-help at stat.math.ethz.ch 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.
> 
> -- 
> Andrew Robinson  
> Department of Mathematics and Statistics            Tel: +61-3-8344-9763
> University of Melbourne, VIC 3010 Australia         Fax: +61-3-8344-4599
> http://www.ms.unimelb.edu.au/~andrewpr
> http://blogs.mbs.edu/fishing-in-the-bay/
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 
> 

-- 
View this message in context: http://www.nabble.com/Numbers-with-correct-significant-digits-tf2657246.html#a7412784
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list