s-news
[Top] [All Lists]

Re: [S] The mysteries of round() and format()?

To: Paul Schwarz <Paul.Schwarz@cornell.edu>
Subject: Re: [S] The mysteries of round() and format()?
From: Prof Brian D Ripley <ripley@stats.ox.ac.uk>
Date: Mon, 31 Jan 2000 07:39:37 +0000 (GMT)
Cc: s-news@wubios.wustl.edu
In-reply-to: <003f01bf6b83$42d227a0$dda95f18@twcny.rr.com>
Sender: owner-s-news@wubios.wustl.edu
On Sun, 30 Jan 2000, Paul Schwarz wrote:

> 
> Hi S-news readers,
> 
> I've been trying to use the round() function along the key() function to
> display regression coefficients in the legend for a plot.  I'd like to
> make sure that all the numbers are formatted to 3 decimal places so that
> the numbers all line up in the legend columns.  Unfortunately, the
> round() function does not do what I thought it would do (or at least not
> what I'd like it to do).  For example:
> 
> > round(2,3)
> [1] 2   # what I'd like is 2.000

> > round(2.0202,3)
> [1] 2.02  # what I'd like printed is 2.020

Careful. Round does do what one would expect: it is the printing that is
supressing the trailing zeroes. key() needs a character string, and will
use as.character (which can be different again).

> I've also tried usin the format() function -- also without success.  How
> do I force the S-PLUS to print the numbers with the 3 decimal places
> (including a trailing zero if need be) so that my number will line up in
> my legend?

Use nsmall in format as in
> format(round(2, 3), nsmall=3)
[1] "2.000"
> format(round(2.02, 3), nsmall=3)
[1] "2.020"

-- 
Brian D. Ripley,                  ripley@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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>