[R] options("digits") and print.default()

Ulrich Keller ulrich.keller at emacs.lu
Mon Jan 15 17:14:20 CET 2007


Hello everyone,

I use latex() (Hmisc) for report generation and thus have been affected by
the problem with rounding decimals described, for example, in this post:

http://thread.gmane.org/gmane.comp.lang.r.general/73287/focus=73287

In short, numbers often are printed with 15 or so decimals even if there
far less significant digits. The problem has been confirmed by Frank
Harrell and Thomas Dupont according to this post:

http://thread.gmane.org/gmane.comp.lang.r.general/73172/focus=73186

But it has still not been fixed. Rather than changing all my reports I
decided I'd look at format.df() in Hmisc myself and try to fix the problem
there. I found that the problem is not in Hmisc at all, but in R itself:

> print(1.001)
[1] 1.001
> options(digits=16) #format.df does this
> print(1.001)
[1] 1.001000000000000
> print(round(1.001, 3)) #rounding does not help
[1] 1.001000000000000

This does not seem to be the behaviour described in the documentation,
which says:

"The same number of decimal places is used throughout a vector,[sic!] This
means that digits specifies the minimum number of significant digits to be
used, and that at least one entry will be encoded with that minimum
number. However, if all the encoded elements then have trailing zeroes,
the number of decimal places is reduced until at least one element has a
non-zero final digit."

If print.default() exhibited the behaviour desribed in the docs,
format.df() and thus latex() would work as advertised, I think. I would
have written a bug report instead of posting here, but the fact (?) that
Frank and Thomas have confirmed the bug seems to indicate that the problem
does indeed lie with Hmisc. Am I misunderstanding something here?

I use R version 2.4.1 Patched (2007-01-13 r40470) on Windows.


Uli



More information about the R-help mailing list