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

From: Toby Marthews <Toby.Marthews_at_lsce.ipsl.fr>
Date: Sun, 15 Jun 2008 11:58:29 +0200 (CEST)

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



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. Received on Sun 15 Jun 2008 - 10:01:42 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 15 Jun 2008 - 16:30:43 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive