rounding - Round up from .5 in R -
yes know why round nearest number if in exact middle (i.e. 2.5 becomes 2) of 2 numbers. when want evaluate data people don't want behaviour. simplest method this:
x <- seq(0.5,9.5,by=1) round(x)
to 1,2,3,...,10 , not 0,2,2,4,4,...,10.
edit: clearify: 1.4999 should 1 after rounding. (i thought obvious)
this not own function, , unfortunately, i can't find got @ moment (originally found anonymous comment @ statistically significant blog), should need.
round2 = function(x, n) { posneg = sign(x) z = abs(x)*10^n z = z + 0.5 z = trunc(z) z = z/10^n z*posneg }
x
object want round, , n
number of digits rounding to.
an example
x = c(1.85, 1.54, 1.65, 1.85, 1.84) round(x, 1) # [1] 1.8 1.5 1.6 1.8 1.8 round2(x, 1) # [1] 1.9 1.5 1.7 1.9 1.8
Comments
Post a Comment