# [R] problems with rounding in output

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Apr 11 08:34:21 CEST 2006

```On Tue, 11 Apr 2006, Brian Quinif wrote:

> Perhaps someone will have a solution to my more general problem, but
> here is the specific one:

These are issues discussed in all good books on R/S programming.
E.g. section 3.5 in MASS.

> I used the round() function to round some estimates to 3 decimal
> places.  I then sent put the rounded estimates in a matrix and used
> latex() to make a LaTeX table from them.  However, in my table, there
> are estimtes which only have 2 decimal places. I assume that the third
> decimal place in these numbers was equal to zero, so the round()
> function simply cut off the superfluous zero.  However, I want all of
> my estimates to have the same number of digits.  Is there a way to
> force R to keep all three digits, even if the last one is zero?

At least three ways:

?format, see argument 'nsmall'.
?sprintf
?formatC

What latex() does (it is not a standard R function, presumably you mean
the one in Hmisc?) with its input is not clear to me, but sending it
character input should resolve this.

> Now, getting to the more general issue, to which a solution might
> negate the need for a solution to the first part of this email.  I
> want to create a table in which all of the standard errors are in
> parentheses.  The solution I used was to input the std. errors into
> the matrix to be used for making the table as follows
>
> paste('(',satt.yr.hrs\$se,')',sep='')
>
> However, after doing so, my LaTeX output had all of the digits that R
> output without rounding like it normally does.

Yes, paste() does an implicit as.character (as it help page says).  R does
_not_ normally round, but print and cat do (in different ways).

> So, I tried using the round() command as described above.  Another
> problem with my solution is that the negative signs in my LaTeX tables
> come out as hyphens, not the usual negative signs from math mode.

So, use math (or tt) mode.  TeX does not use different characters for
hyphen and minus (even though they are different characters in Unicode,
Adobe encodings ...), so I do not see how this can be solved in R.

--
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

```