Glenn E Stauffer
2009-May-20 20:10 UTC
[R] round function seems to produce maximum 2 decimals
I am trying to use round()to force R to display a specific number of decimals, but it seems to display <=2 decimals no matter what I specify in the digits argument. As an alternative I tried signif(), but it also produces unexpected results. See example code and results below. Format() works, but then the result no longer is numeric. Am I missing something simple? I am using R 2.9.0 on Windows XP. Thanks, Glenn #code h=12345.16711 h round(h,digits=1) round(h,digits=2) round(h,digits=3) round(h,digits=4) round(h,digits=5) signif(h,digits=9) format(h,nsmall=4) #results> h=12345.16711 > h[1] 12345.17> round(h,digits=1)[1] 12345.2> round(h,digits=2)[1] 12345.17> round(h,digits=3)[1] 12345.17> round(h,digits=4)[1] 12345.17> round(h,digits=5)[1] 12345.17> signif(h,digits=9)[1] 12345.17> > format(h,nsmall=4)[1] "12345.1671"
(Ted Harding)
2009-May-20 21:54 UTC
[R] round function seems to produce maximum 2 decimals
On 20-May-09 20:10:15, Glenn E Stauffer wrote:> I am trying to use round()to force R to display a specific number > of decimals, but it seems to display <=2 decimals no matter what I > specify in the digits argument. As an alternative I tried signif(), > but it also produces unexpected results. See example code and results > below. > Format() works, but then the result no longer is numeric. Am I missing > something simple? > I am using R 2.9.0 on Windows XP. > Thanks, > Glenn > >#code > h=12345.16711 > h > > round(h,digits=1) > round(h,digits=2) > round(h,digits=3) > round(h,digits=4) > round(h,digits=5) > > signif(h,digits=9) > > format(h,nsmall=4) > >#results >> h=12345.16711 >> h > [1] 12345.17 >> round(h,digits=1) > [1] 12345.2 >> round(h,digits=2) > [1] 12345.17 >> round(h,digits=3) > [1] 12345.17 >> round(h,digits=4) > [1] 12345.17 >> round(h,digits=5) > [1] 12345.17 >> signif(h,digits=9) > [1] 12345.17 >> >> format(h,nsmall=4) > [1] "12345.1671"What you're missing is that when you do (e.g.) h <- 12345.16711 round(h,digits=4) # [1] 12345.17 what is displayed ("[1] 12345.17") is not the result of round(), but what the result of round() is to be displayed as given the options digits=7 (default) for the number of *significant figures* in the display of stored values. To see the result as it is stored, you should use print() with the appropriate number of disgits specified: print( round(h,digits=5),10) # [1] 12345.16711 print( round(h,digits=4),10) # [1] 12345.1671 print( round(h,digits=3),10) # [1] 12345.167 print( round(h,digits=2),10) # [1] 12345.17 Internally, round(h) is correctly stored: h4 <- round(h,4) h - h4 # [1] 1e-05 h3 <- round(h,3) h - h3 # [1] 0.00011 h2 <- round(h,2) h - h2 # [1] -0.00289 To illustrate the influence of the display option digits=7: h<-45.16711 h # [1] 45.16711 round(h,digits=4) # [1] 45.1671 round(h,digits=3) # [1] 45.167 round(h,digits=2) # [1] 45.17 h<-345.16711 h # [1] 345.1671 round(h,digits=4) # [1] 345.1671 round(h,digits=3) # [1] 345.167 round(h,digits=2) # [1] 345.17 h<-2345.16711 h # [1] 2345.167 round(h,digits=4) # [1] 2345.167 round(h,digits=3) # [1] 2345.167 round(h,digits=2) # [1] 2345.17 Hoping this helps, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 20-May-09 Time: 22:54:41 ------------------------------ XFMail ------------------------------
Glenn E Stauffer wrote:> > I am trying to use round()to force R to display a specific number of > decimals, but it seems to display <=2 decimals no matter what I specify in > the digits argument. As an alternative I tried signif(), but it also > produces unexpected results. See example code and results below. Format() > works, but then the result no longer is numeric. Am I missing something > simple? > I am using R 2.9.0 on Windows XP. > Thanks, > Glenn > > #code > h=12345.16711 > h > > round(h,digits=1) > round(h,digits=2) > round(h,digits=3) > round(h,digits=4) > round(h,digits=5) > > signif(h,digits=9) > > format(h,nsmall=4) > > #results >> h=12345.16711 >> h > [1] 12345.17 >> round(h,digits=1) > [1] 12345.2 >> round(h,digits=2) > [1] 12345.17 >> round(h,digits=3) > [1] 12345.17 >> round(h,digits=4) > [1] 12345.17 >> round(h,digits=5) > [1] 12345.17 >> signif(h,digits=9) > [1] 12345.17 >> >> format(h,nsmall=4) > [1] "12345.1671" > >options("digits") is set to 7 by default.> h <- 12345.16711 > round(h,digits=5)[1] 12345.17> print(h,digits=12)[1] 12345.16711> options(digits=12) > round(h,digits=5)[1] 12345.16711> round(h,digits=4)[1] 12345.1671> round(h,digits=3)[1] 12345.167> round(h,digits=2)[1] 12345.17 -- View this message in context: http://www.nabble.com/round-function-seems-to-produce-maximum-2-decimals-tp23643311p23643853.html Sent from the R help mailing list archive at Nabble.com.