[R] Digits in R

William Dunlap wdunlap at tibco.com
Tue Mar 25 20:17:10 CET 2014


> The round() function lets you specify the number of decimal
> places directly:
> 
> round(vars, 4)

round() and print() don't always agree on meaning of "the
number of digits".  In addition, rounding is not a transitive
operation - rounding a rounded number does not always
give you a rounding of the original number.
E.g., with R-3.0.2 on Windows:
   >  print(round(9/11, digits=3), digits=16)
   [1] 0.8179999999999999
   > print(round(1.11111146, digits=7), digits=7)
   [1] 1.111112

If you want to control the number of printed digits it is better to
control print() itself, not use round().

Bill Dunlap
TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of David Carlson
> Sent: Tuesday, March 25, 2014 7:02 AM
> To: 'Qiang Kou'; 'Pavneet Arora'
> Cc: 'R help'
> Subject: Re: [R] Digits in R
> 
> The digits option gives you the number of significant digits
> (ignoring leading zeros after the decimal) and it operates on a
> column by column basis. The first and second columns have values
> with a leading zero after the decimal (-0.04074 in both columns)
> so R prints five decimal places to get four significant digits
> (4074). In the third and fourth columns four decimals are enough
> so R uses that.
> 
> The round() function lets you specify the number of decimal
> places directly:
> 
> round(vars, 4)
> 
> To set and reset the "digits" option:
> 
> oldpar <- options(digits=4) # Set digits and save the default
> value
> 
> # Commands using digits=4
> 
> options(oldpar) # Resets to the default value
> 
> -------------------------------------
> David L Carlson
> Department of Anthropology
> Texas A&M University
> College Station, TX 77840-4352
> 
> 
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Qiang Kou
> Sent: Tuesday, March 25, 2014 8:45 AM
> To: Pavneet Arora
> Cc: R help
> Subject: Re: [R] Digits in R
> 
> Have you tried "round(vars, digits = 4)" ?
> 
> KK
> 
> 
> On Tue, Mar 25, 2014 at 7:17 AM, Pavneet Arora <
> pavneet.arora at uk.rsagroup.com> wrote:
> 
> > Hello All,
> >
> > I am new in R, so please excuse the basic questions.
> > But how do you change the number of digits to be displayed?
> >
> > I have done the following, hoping I will get 4 decimal places
> in my
> > answer:
> >
> > vars <- var(cbind(Sepal.l,Sepal.w,Petal.l,Petal.w))
> > print(vars,digits=4)
> >
> > But as seen below from the output, that is not the case
> >
> > > print(vars,digits=4)
> >          Sepal.l  Sepal.w Petal.l Petal.w
> > Sepal.l  0.68569 -0.04074  1.2743  0.5163
> > Sepal.w -0.04074  0.19363 -0.3287 -0.1212
> > Petal.l  1.27432 -0.32873  3.1163  1.2956
> > Petal.w  0.51627 -0.12124  1.2956  0.5810
> >
> > How come the Sepal.l and Sepal.w does not come with only 4
> decimal places.
> > Can someone tell me where I am going wrong?
> >
> > Also if I use the "options(digits=4)"- How do I make it to go
> back to
> > Normal or default, which is 7 in this case.?
> >
> >
> ****************************************************************
> ****************************************************************
> ****************************************************************
> ***********************
> > MORE TH>N is a trading style of Royal & Sun Alliance Insurance
> plc (No.
> > 93792). Registered in England and Wales at St. Mark's Court,
> Chart Way,
> > Horsham, West Sussex, RH12 1XL.
> >
> > Authorised by the Prudential Regulation Authority and
> regulated by the
> > Financial Conduct Authority and the Prudential Regulation
> Authority.
> >
> >
> ****************************************************************
> ****************************************************************
> ****************************************************************
> ************************
> >
> >         [[alternative HTML version deleted]]
> >
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible
> code.
> >
> >
> 
> 
> --
> Qiang Kou
> qkou at umail.iu.edu
> School of Informatics and Computing, Indiana University
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible
> code.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list