The R Arithmetic operators include operators like Arithmetic Addition, Subtraction, Division, Multiplication, Exponent, Integer Division, and Modulus. These arithmetic operators are binary, meaning they operate on two operands. The table below shows all the Arithmetic Operators in R Programming language with examples.
|R Arithmetic Operators||Operation||Example|
|+||Addition||15 + 5 = 20|
|–||Subtraction||15 – 5 = 10|
|*||Multiplication||15 * 5 = 75|
|/||Division||15 / 5 = 3|
|%/%||Integer Division – Same as Division. but it returns the integer value by flooring the extra decimals||16 %/% 3 = 5. If you divide 16 by 3 you get 5.333, but the Integer division operator trims the decimal values and outputs the integer|
|^||Exponent – It returns the Power of One variable against the other||15 ^ 3 = 3375 (It means 15 Power 3 or 103).|
|%%||Modulus – It returns the remainder after the division||15 %% 5 = 0 (Here remainder is zero). If it is 17 %% 4, then result = 1.|
R Arithmetic Operators Example
In this R Programming arithmetic operators example, we use two variables, a and b, whose values are 16 and 3. Here, we are going to use these two variables to perform various arithmetic operations present in the R programming language
# Example a <- 16 b <- 3 add <- a + b sub = a - b multi = a * b division = a / b Integer_Division = a %/% b exponent = a ^ b modulus = a %% b print(paste("Addition of two numbers 16 and 3 is : ", add)) print(paste("Subtracting Number 3 from 16 is : ", sub)) print(paste("Multiplication of two numbers 16 and 3 is : ", multi)) print(paste("Division of two numbers 16 and 3 is : ", division)) print(paste("Integer Division of two numbers 16 and 3 is : ", Integer_Division)) print(paste("Exponent of two numbers 16 and 3 is : ", exponent)) print(paste("Modulus of two numbers 16 and 3 is : ", modulus))
When we are using the division ( / ) operator, the result is a float or decimal value. If you want to display the output as an integer value by rounding the value, then use R Integer Division ( %/% ) operator. The following R Programming statement finds the exponent. It means 16 power 3 = 16 * 16 * 16 = 4096.
exponent = a ^ b