Yes I know why we always round to the nearest even number if we are in the exact middle (i.e. 2.5 becomes 2) of two numbers. But when I want to evaluate data for some people they don't want this behaviour. What is the simplest method to get this:
x <- seq(0.5,9.5,by=1)
round(x)
to be 1,2,3,...,10 and not 0,2,2,4,4,...,10.
Edit: To clearify: 1.4999 should be 1 after rounding. (I thought this would be obvious)
If you want something that behaves exactly like
round
except for those xxx.5 values, try this:Depending on how comfortable you are with jiggling your data, this works:
The best and most simple solution is to add 0.00000001 to the number and then round it.
round(1.15 + 0.00000001, 1) = 1.2
As @CarlWitthoft said in the comments, this is the IEC 60559 standard as mentioned in
?round
:An additional explanation by Greg Snow:
This appears to work:
Ananda Mahto's response seems to do this and more - I am not sure what the extra code in his response is accounting for; or, in other words, I can't figure out how to break the rnd() function defined above.
Example:
This is not my own function, and unfortunately,
I can't find where I got it at the moment(originally found as an anonymous comment at the Statistically Significant blog), but it should help with what you need.x
is the object you want to round, andn
is the number of digits you are rounding to.An Example