R Arithmetic Operators

Arithmetic operators perform mathematical calculations in R. Every data analysis involves calculations — totals, averages, percentages, differences. Knowing all arithmetic operators and how they behave with different data types is a core skill.

All Arithmetic Operators in R

Operator   Name                  Example    Result
──────────────────────────────────────────────────────
+          Addition              8 + 3      11
-          Subtraction           8 - 3      5
*          Multiplication        8 * 3      24
/          Division              8 / 3      2.666...
^          Exponentiation        8 ^ 3      512
**         Exponentiation (alt)  8 ** 3     512
%%         Modulo (remainder)    8 %% 3     2
%/%        Integer division      8 %/% 3    2

Understanding Modulo and Integer Division

These two operators confuse beginners. Here is a simple diagram:

    8 ÷ 3  =  2  remainder  2
              ↑               ↑
           8 %/% 3          8 %% 3
         (integer part)   (remainder)

Real example: 
  17 eggs, packing into boxes of 6
  17 %/% 6  = 2 full boxes
  17 %% 6   = 5 eggs left over
17 %/% 6   # 2
17 %% 6    # 5

Operator Precedence (Order of Operations)

R follows the standard math order: brackets first, then powers, then multiplication/division, then addition/subtraction.

3 + 4 * 2      # 11  (multiply first, then add)
(3 + 4) * 2    # 14  (bracket forces addition first)
2 ^ 3 + 1      # 9   (power first: 8 + 1)
2 ^ (3 + 1)    # 16  (bracket first: 2^4)
Precedence (highest to lowest):
1. ( )  — brackets
2. ^    — exponentiation
3. * /  — multiplication and division (left to right)
4. + -  — addition and subtraction (left to right)

Arithmetic on Vectors

R performs arithmetic element-by-element on vectors — this is called vectorized operation and is one of R's most powerful features.

prices  <- c(100, 200, 150, 300)
tax     <- 0.18

# Apply 18% tax to every price at once
prices_with_tax <- prices + (prices * tax)
print(prices_with_tax)

Output:

[1] 118 236 177 354

No loop needed. R multiplied each price by 0.18 and added it automatically.

Vector Recycling

When two vectors have different lengths, R reuses (recycles) the shorter one.

a <- c(10, 20, 30, 40)
b <- c(1, 2)

a + b
# [1] 11 22 31 42
# b was recycled: 1,2,1,2 added to 10,20,30,40
Diagram:
  a:  10  20  30  40
  b:   1   2   1   2   ← b recycled
  ─────────────────────
  +:  11  22  31  42

Practical Example: Salary Calculation

# Monthly salary components (in rupees)
basic_salary    <- 40000
hra             <- basic_salary * 0.40    # 40% of basic
travel_allow    <- 3000
pf_deduction    <- basic_salary * 0.12    # 12% PF

gross_salary    <- basic_salary + hra + travel_allow
net_salary      <- gross_salary - pf_deduction

cat("Gross Salary: ₹", gross_salary, "\n")
cat("PF Deduction: ₹", pf_deduction, "\n")
cat("Net Salary:   ₹", net_salary, "\n")

Output:

Gross Salary: ₹ 59000
PF Deduction: ₹ 4800
Net Salary:   ₹ 54200

Math Functions That Complement Operators

Function    Description              Example
──────────────────────────────────────────────────
abs(x)      Absolute value           abs(-9) = 9
sqrt(x)     Square root              sqrt(25) = 5
log(x)      Natural logarithm        log(exp(1)) = 1
log10(x)    Log base 10              log10(1000) = 3
log2(x)     Log base 2               log2(8) = 3
exp(x)      e raised to power x      exp(1) = 2.718...
factorial(x) Factorial               factorial(5) = 120

Arithmetic operators are the building blocks of every calculation in R. Combined with vectors and functions, they let you process entire datasets in a single line of code.

Leave a Comment

Your email address will not be published. Required fields are marked *