[R] Max sig figs as well as Min in print?
Prof Brian Ripley
ripley at stats.ox.ac.uk
Thu Jun 5 17:14:18 CEST 2003
On Thu, 5 Jun 2003 Ted.Harding at nessie.mcc.ac.uk wrote:
> Hi Folks,
>
> Consider the following example (artificial, but it illustates the point):
>
> > r2<-sqrt(2)
> > x<-2-r2*r2
>
> > print(c(pi,sqrt(pi)),digits=5)
> [1] 3.1416 1.7725
>
>
> > print(c(pi,sqrt(pi),x),digits=5)
> [1] 3.1416e+00 1.7725e+00 -4.4409e-16
>
> whereas I would prefer
>
> [1] 3.1416 1.7725 0.0000
>
> which could be achieved by
>
> > print(c(pi,sqrt(pi),round(x)),digits=5)
> [1] 3.1416 1.7725 0.0000
>
> if need be.
>
> The above of course is due to the definition of "digits" as the miniumum
> number of significant figures to print. In order to avoid the behaviour of
> the second "print" and make it like the last line, one would need to be
> able to set somthing like "maxdigits=5" as well as "digits=5".
>
> Is anything of this sort possible (apart from anticipating the situation
> and wrapping relevant computations in "round")?
Might zapsmall help?
> print(zapsmall(c(pi,sqrt(pi),x)), digits=5)
[1] 3.1416 1.7725 0.0000
Alternatively,
sprintf("%8.4f %8.4f %8.4f", pi,sqrt(pi),x)
formatC(c(pi,sqrt(pi),x), digits=4, format="f")
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list