[R] Max sig figs as well as Min in print?

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Thu Jun 5 15:27:19 CEST 2003


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")?

Thanks, and best wishes,
Ted.




More information about the R-help mailing list