The modulo operator (%% in R) returns the remainder of the division of 2 numbers.
Here are some examples:
- 5 %% 2 returns 1, because 2 goes into 5 two times and the remainder is 1 (i.e. 5 = 2 × 2 + 1).
- 4 %% 2 returns 0, since 4 = 2 × 2 + 0.
- 4 %% 1 returns 0, since 4 = 4 × 1 + 0.
- 2 %% 4 returns 2, since 2 = 0 × 4 + 2.
- 0 %% 2 returns 0, since nothing remains from dividing 0 by 2 (i.e. 0 = 0 × 2 + 0).
- 2 %% 0 returns NaN, since we cannot divide by zero.
The modulo operator also works with decimal numbers, but this is rarely used in practice:
- 5.6 %% 3.2 returns 2.4, since 5.6 = 1 × 3.2 + 2.4.
- 5.6 %% 3 returns 2.6.
- 5.6 %% 1 returns 0.6.
Below are 5 practical problems that can be solved using the modulo operator:
1. Checking if a number is even or odd
Problem: Is 27 even or odd?
Solution:
n = 27 # if n is divisible by 2 then: n %% 2 == 0 # must be TRUE print(paste('n is', ifelse(n %% 2 == 0, 'even', 'odd'))) # this code prints: "n is odd"
2. Checking if a number is prime
A prime number is any integer (other than 0 and 1) divisible only by 1 and itself without a remainder.
For example:
- 5 is a prime number since it is only divisible by 1 and 5.
- 4 is not a prime number since it can also be divided by 2.
Problem: Is 5 a prime number?
Solution:
n = 5 # 5 is not prime if 5 %% 2, 5 %% 3, or 5 %% 4 is zero notPrime = any(n %% 2:(n-1) == 0) print(paste('n is', ifelse(notPrime, 'not a prime nb', 'a prime nb'))) # this code prints: "n is a prime nb"
3. Systematic sampling from a population
Systematic sampling consists of select every kth person from a population of interest.
Problem: Select every 7th person from a list of 100 people. What is the sample size?
Solution:
population = 1:100 sample = population[population %% 7 == 0] print(sample) # outputs: 7 14 21 28 35 42 49 56 63 70 77 84 91 98 # sample size length(sample) # outputs: 14
4. Creating equal subgroups from a larger group
Problem: A company has 507 employees. How many teams of equal size can we create?
Solution:
# which numbers can 507 be divided by without a remainder? which(507 %% 1:507 == 0) # outputs: 1 3 13 39 169 507
So, here are the possible groups:
- 1 group of 507 employees, or
- 3 groups of 169 (= 507/3) employees, or
- 13 groups of 39 (= 507/13) employees, or
- 39 groups of 13 employees, or
- 169 groups of 3 employees, or
- 507 groups of 1 employee.
5. Converting age from days to: years, months, and days
Problem: Report the age in the format: “years, months, days” of a person aged 500 days.
Solution:
# age in days days = 500 # how many years are there in 500 days? years = floor(days / 365) # the floor function rounds down print(years) # 1 remainder = days %% 365 # days remaining print(remainder) # 135 # how many months are there in the remaining 135 days? months = floor(remainder / 30) print(months) # 4 remainder = remainder %% 30 # days remaining print(remainder) # 15 # remaining days days = remainder # age print(paste(years, 'year(s),', months, 'month(s),', days, 'day(s)')) # this code prints: "1 year(s), 4 month(s), 15 day(s)"