[R] Several Basic Questions

Minghua Yao myao at ou.edu
Tue May 20 17:17:13 CEST 2003


Dear Prof. Ripley,

Thank you for your reply. It works fine except for question 3. I got

> cat(format(round(z, 2)), "\n")
1134567

May variable z has been rounded to 0 digit behind the point at the very
beginning. How to avoid that?

Minghua

-----Original Message-----
From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk]
Sent: Tuesday, May 20, 2003 9:12 AM
To: Minghua Yao
Cc: R Help
Subject: Re: [R] Several Basic Questions


On Tue, 20 May 2003, Minghua Yao wrote:

> I am having several basic questions that I haven't found the answer to
from
> the manuals:
>
> 1. How to remove "[1]" when a single line message is printed?

Use cat()

> 2. How to print several variables (e.g., a character string and a numeric
> variable) at the same line?

Use cat()

> 3. How to have the control of the accuracy of variables? e.g., in the
> following,
>
> > x<-1134567.1
> > y<-0.19
> > z<-x-y
> > z
> [1] 1134567
> >
>
> I want variable to have an accuracy of 2 digits behind the decimal point
> instead of all the digits behind the point being cut off.

Use cat, round and format:

> cat(format(round(z, 2)), "\n")
1134566.91


These may not be explicit in the R manuals, but they are in all good books
on R (see the FAQ).

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