0.08953 to two significant figures is "0.090" not "0.09".
 
Thanks, I'll sprintf instead.
 
From: Duncan Murdoch <murdoch.duncan@gmail.com>
To:Steve Taylor <steve.taylor@aut.ac.nz>
CC:<r-help@r-project.org>
Date: 29/Apr/2010 9:53a
Subject: Re: [R] unexpected result from format(x, digits)
On 28/04/2010 5:45 PM, Steve Taylor wrote:
>  
> Is this a bug somewhere?  The format function, using a specific number of digits, doesn't give sensible results:
>   

I don't see the error.  In all examples, the smallest value is 0.08953, 
and it appears to be rendered to the requested number of significant 
figures in each case.  All the other numbers are rendered to match.

If you want to specify decimal places rather than significant digits, 
you should probably use sprintf() rather than format().

Duncan Murdoch
>  
> R> set.seed(2);print(x<-rexp(5))
> [1] 1.86535 0.40475 0.14665 1.73071 0.08953
> R> format(x,digits=1)
> [1] "1.87" "0.40" "0.15" "1.73" "0.09"
> R> format(x,digits=2)
> [1] "1.87" "0.40" "0.15" "1.73" "0.09"
> R> format(x,digits=3)
> [1] "1.8654" "0.4047" "0.1467" "1.7307" "0.0895"
   


	[[alternative HTML version deleted]]

