Rounding in R

The round() function in Base R will round to the nearest whole number and ‘rounding to the even number’ when equidistant, meaning that exactly 12.5 rounds to the integer 12.

Note that the janitor package in R contains a function round_half_up() that rounds away from zero. in this case it rounds to the nearest whole number and ‘away from zero’ or ‘rounding up’ when equidistant, meaning that exactly 12.5 rounds to the integer 13.

#Example code
my_number <-c(2.2,3.99,1.2345,7.876,13.8739)

r_0_dec <- round(my_number, digits=0);
r_1_dec <- round(my_number, digits=1);
r_2_dec <- round(my_number, digits=2);
r_3_dec <- round(my_number, digits=3);

r_0_dec
r_1_dec
r_2_dec
r_3_dec

> r_0_dec
[1]  2  4  1  8 14
> r_1_dec
[1]  2.2  4.0  1.2  7.9 13.9
> r_2_dec
[1]  2.20  3.99  1.23  7.88 13.87
> r_3_dec
[1]  2.200  3.990  1.234  7.876 13.874

If using the janitor package in R, and the function round_half_up(), the results would be the same with the exception of rounding 1.2345 to 3 decimal places where a result of 1.235 would be obtained instead of 1.234.