The R Arithmetic operators include operators like Arithmetic Addition, Subtraction, Division, Multiplication, Exponent, Integer Division and Modulus. All these operators are binary operators, which means they operate on two operands. Below table 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 will return the integer value by flooring the extra decimals||16 %/% 3 = 5. If you divide 16 with 3 you get 5.333 but the Integer division operator will trim 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 it will be 1.|
R Arithmetic Operators Example
In this example, We are using two variables a and b and their values are 16 and 3. We are going to use these two variables to perform various arithmetic operations present in the R programming language
# Example for R Arithmetic Operators 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))
The following statement will find the exponent. It means 16 power 3 = 16 * 16 * 16 = 4096
exponent = a ^ b
NOTE: When we are using division ( / ) operator the result will be float or decimal value. If you want to display the output as integer value by rounding the value then use Integer Division ( %/% ) operator
Thank You for Visiting Our Blog.