Modulo in r – Why you Need the modulo operator in R

In R programming when doing integer division, you may want to format the result as quotient and remainder. However, it needs to be noted that when you do modulo in R to get the remainder that this is an operation and not a function. It is a straightforward process, which produces a simple result.

Description

In many programming languages the mod function is the way to obtain the remainder. However, in R programming the modulo operator is the equivalency of the mod operation. The modulus operator has the format of n %% d, where “n” is the dividend, “d” is the divisor, and “%%” indicates the operation being performed. It is accompanied by a similar operation with the format of n %/% d which produces an integer quotient. Between the two of them, we are able to obtain both the integer quotient and remainder when dividing two integers.

When these operations are used in connection with the multiplicative inverse, it produces an interesting result. This is because the multiplicative inverse of any number greater than one is a fraction that can be expressed in the form of 1/d. Because of this, using the two operations 1 %/% d and 1 %% d produces results of zero and one respectively, except in the case where d = 1 then the results are one and zero respectively.

Explanation

The remainder resulting from integer division is a basic part of number theory. The modulo operation is related to division but simply provides the difference between the divisor and the last previous number evenly divided by the dividend. For example, the operation 12 %% 5 produces two because five evenly divides into ten and the difference between ten and twelve is two. This is a straightforward process which is why it is commonly performed in programming using an operation rather than a function.

This also explains the pattern that is produced when working with the multiplicative inverse. Any number greater than one will divide one exactly zero times, and the difference between zero and one is one. On the other hand, one divides one exactly once, and the difference between one and one is zero.

When you are doing integer division it is often important to be able to get both the integer quotient and remainder. This is because neither is really an answer by itself, but combined they form a complete answer.

Examples

Here are three code examples that show the modulo operation in action, each of them is using different situations. Together, they provide an illustration of this operation and its related operations being used with three different data types.

> x = 30
> y = 7

> x/y
[1] 4.285714

> x %/% y
[1] 4

> x %% y
[1] 2

We apply this operation to a pair of simple numeric variables in this example.

> t = as.numeric(Sys.time())
> set.seed(t)
> x = as.integer(abs(rnorm(7)*20))
> y = as.integer(abs(rnorm(7)*10)+1)

> x
[1] 36 23 29 17 45 21 18

> y
[1] 5 5 8 9 21 7 3

> x/y
[1] 7.200000 4.600000 3.625000 1.888889 2.142857 3.000000 6.000000

> x %/% y
[1] 7 4 3 1 2 3 6

> x %% y
[1] 1 3 5 8 3 0 0

In this example, we apply this operation to a pair of vectors. We are dividing one numeric vector by another.

> t = as.numeric(Sys.time())
> set.seed(t)

> x = data.frame(A = as.integer(abs(rnorm(7)*20)),
+ B = as.integer(abs(rnorm(7)*20)),
+ C = as.integer(abs(rnorm(7)*20)))

> y = data.frame(A = as.integer(abs(rnorm(7)*10)+1),
+ B = as.integer(abs(rnorm(7)*10)+1),
+ C = as.integer(abs(rnorm(7)*10)+1))

> x
A B C
1 36 8 25
2 23 9 18
3 29 15 33
4 17 16 2
5 45 41 9
6 21 12 2
7 18 5 15

> y
A B C
1 19 3 20
2 6 6 9
3 8 13 2
4 15 3 13
5 1 24 8
6 3 5 28
7 12 7 9

> x/y
A B C
1 1.894737 2.6666667 1.25000000
2 3.833333 1.5000000 2.00000000
3 3.625000 1.1538462 16.50000000
4 1.133333 5.3333333 0.15384615
5 45.000000 1.7083333 1.12500000
6 7.000000 2.4000000 0.07142857
7 1.500000 0.7142857 1.66666667

> x %/% y
A B C
1 1 2 1
2 3 1 2
3 3 1 16
4 1 5 0
5 45 1 1
6 7 2 0
7 1 0 1

> x %% y
A B C
1 17 2 5
2 5 3 0
3 5 2 1
4 2 1 2
5 0 17 1
6 0 2 2
7 6 5 6

We apply this operation to a pair of data frames in this example. We are dividing one numeric data frame by another. Combined these examples illustrate the modulo operation being used multiple times.

Application

There are many applications of modular arithmetic. There are situations where having an even division with a remainder comes in handy. Called Euclidian division, one application is the situation where you have a set number of rooms, and you have to divide a group of people among them. In such a situation you want to know how many people to assign to each room and whether or not there are going to be any leftover. Another application would be finding congruent modulo values among serial numbers for the purposes of producing groups. This process finds application in any place where you have discrete units that need the integer quotient, rather than the absolute value quotient, which more often than not is a fraction. Modular arithmetic is handy anytime you are dividing objects into even groups such that you might have some leftovers. That is basically what remainders are mathematically, they are the leftovers of dividing a collection of objects into equally sized groups.

The modulo operator is a handy tool in data science for any situation where you are dividing integer values and need to find the remainders. It is helpful in any situation where you are dividing objects into equal-size groups and need to know how many you have leftover. It is an operation that is easy to learn and make use of.

Scroll to top
Privacy Policy