[R] round(1.5) = round(2.5) = 2?

Toby Marthews Toby.Marthews at lsce.ipsl.fr
Sun Jun 15 11:58:29 CEST 2008


Hi Markus,

The R function round() uses the round-to-even method, which is explained
on http://en.wikipedia.org/wiki/Rounding#Round-to-even_method.

If you would like instead "traditional rounding" then you should add 0.5
and take the integer part, as is suggested in the examples on ?round, e.g.

x=c(1.49,1.50,1.51,2.49,2.50,2.51,3.49,3.50,3.51);r2e=round(x);trad=floor(x+0.5);data.frame(x,r2e,trad)

The reason for the IEEE standard is to do with signal processing and the
bias introduced by traditional rounding if you have a lot of data points
whose decimal expansion ends in ....5.

Personally, I am a mathematician and satistician and I find that in 99% of
cases traditional rounding is what is required (basically always except
some very specific examples involving very large sets of data) so for R to
have put the IEEE standard as the default (rather than, say, an option) is
a bit odd. However, R's benefits and advantages by far outweigh its little
oddities, as I presume you know since you are using it.

Effectively, I never use the round() command and always calculate using
the floor function.

Toby Marthews


Le Dim 15 juin 2008 11:26, Markus Didion a écrit :
> Dear R-users
>
> with a bit of grief I had to repeat an extensive analysis because I
> did not suspect (and therefore did not read the documentation) that
> round was implemented as "for rounding off a 5, the IEC 60559 standard
> is expected to be used, 'go to the even digit'", resulting in
> round(1.5) = 2
> round (2.5) = 2.
>
> As a non-mathematician I am both puzzled and intrigued by this rule as
> it is against what I have learned in my math courses, i.e.
> round(1.5) = 2
> round (2.5) = 3.
>
> I would like to understand the reason behind this rule.
>
> Thanks for your comments.
>
> Markus
>
> --
>
> Markus Didion
>
> Wald?kologie						Forest Ecology
> Inst. f. Terrestrische Oekosysteme		Inst. of Terrestrial Ecosystems
> Departement Umweltwissenschaften	Dept. of Environmental Sciences
> Eidg. Technische Hochschule			Swiss Fed. Inst. of Technology
> ETH-Zentrum CHN G78				ETH-Zentrum CHN G78
> Universit?tstr. 22						Universitaetstr. 22
> CH-8092 Z?rich						CH-8092 Zurich
> Schweiz								Switzerland
>
> Tel +41 (0)44 632 5629		Fax +41 (0)44 632 1358
> Email markus.didion at env.ethz.ch
> homepage: http://www.fe.ethz.ch/people/didionm



More information about the R-help mailing list