[R] rounding problem

Bernardo Rangel Tura tura at centroin.com.br
Mon Mar 2 10:27:47 CET 2009


On Sun, 2009-03-01 at 17:51 -0800, tedzzx wrote:
> Yes, round(1.5)=2. but round(2.5)=2.  I want round(2.5)=3 just like the what
> the excel do.  Can we change the setting or do some trick so that the
> computer will work like what we usually do with respect to rounding.
> My system is R 2.8.1, winXP, Intel core 2 dual . Thanks. 
On Sun, 2009-03-01 at 17:51 -0800, tedzzx wrote:
> Yes, round(1.5)=2. but round(2.5)=2.  I want round(2.5)=3 just like
> the what
> the excel do.  Can we change the setting or do some trick so that the
> computer will work like what we usually do with respect to rounding.

If I understand your problem you need using a bias round like excell.
Do you understand this is a wrong way and this is a bug of excell, but
you need this.

Well to help you i create a function for this

round.excell<-function(x){
        aux<-round(x,1)
        aux<-ifelse(
                all.equal(aux-trunc(aux),.5)==TRUE,
                ceiling(aux),
                round(aux,0))
        return(aux)
}
round.excell(1.5)
round.excell(2.5)
round.excell(2.1)
round.excell(1.1)
round.excell(2.6)
round.excell(1.6)

But I need talk to you:

1- The use of routine put a bias in your analysis
2- Is not a standard (check IEC 60559 about this)
3- Do you migrating a excell bug to R
-- 
Bernardo Rangel Tura, M.D,MPH,Ph.D
National Institute of Cardiology
Brazil




More information about the R-help mailing list