[R] round function seems to produce maximum 2 decimals

Ben Bolker bolker at ufl.edu
Wed May 20 23:59:08 CEST 2009




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.




More information about the R-help mailing list