StATS: Modular arithmetic and rounding in R (February 1, 2007)
In certain programming situations, you need to perform calculations involving division that produce whole numbers as a result. For example, if you divided 27 by 4, you would get 6.75, but if you were using whole numbers only, then your result would be 6 with a remainder of 3.
In R, the operator
%/% produces an integer division, and the operator
%% computes the remainder. So in R, the result of
19%/%4 would be
and the result of
19%%4 would be
Here's a practical example. Suppose you wanted to take the numbers 0 through 23 and place them in a matrix with 4 rows and 6 columns, filling the matrix row by row. Integer division will help you assign the rows
 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6
and modular artithmetic will help you assign the columns
 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
If you want to round a number to the nearest integer, use the
function. For numbers with fractional portions exactly equaling 0.5, this function always
rounds to the even digit. The
round() function has an argument,
which allows you to round at a different level of precision. For example, using
digits=2 will round to the nearest hundredth, and
digits=-3 will round to
the nearest thousand.
Other related functions that will produce integer results are
ceiling()which computes the closest integer that is larger than x,
floor()which computes the closest integer that is smaller than x, and
trunc()which computes an integer by dropping the fractional portion.
You would think that the last two functions produce the same results, and that is indeed true for positive numbers. But for negative numbers, dropping the fractional portion will actually produce a slightly larger value. Thus
Related web pages:
This page was written by Steve Simon while working at Children's Mercy Hospital. Although I do not hold the copyright for this material, I am reproducing it here as a service, as it is no longer available on the Children's Mercy Hospital website. Need more information? I have a page with general help resources. You can also browse for pages similar to this one at Category: R software.