R Academy · Lesson

Arithmetic and Comparison Operators

Learn +, -, *, /, %%, %/% and comparison operators in R.

Lesson 1 of 413 steps

Arithmetic and Comparison Operators is a free R Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the R Academy learning path, and your progress syncs across the web and the CoddyKit app. The R Academy course includes 4 lessons in total.

Introduction to R Operators

Operators are symbols that tell R to perform specific operations. In this lesson, we explore arithmetic and comparison operators — the foundation of any R program.

R supports all standard math operations plus some unique ones like modulo and integer division.

# Basic arithmetic in R
x <- 10
y <- 3
cat('x =', x, ', y =', y, '
')

Addition and Subtraction

The + and - operators work exactly as in mathematics. They can be applied to single values or entire vectors at once.

price <- 49.99
discount <- 5.00
final_price <- price - discount
tax <- final_price + 3.50
cat('Final price with tax:', tax, '
')

Multiplication and Division

Use * for multiplication and / for division. Division in R always returns a decimal (double), even when dividing whole numbers.

width <- 8
height <- 5
area <- width * height
half_area <- area / 2
cat('Area:', area, '
')
cat('Half area:', half_area, '
')
cat('7 / 2 =', 7 / 2, '
')  # returns 3.5

Exponentiation with ^

Use ^ (or **) to raise a number to a power. This is useful for scientific calculations and polynomial expressions.

side <- 4
area_square <- side ^ 2
cat('Area of square:', area_square, '
')

base <- 2
cat('2^8 =', base ^ 8, '
')
cat('Square root of 16:', 16 ^ 0.5, '
')

Modulo Operator %%

The %% operator returns the remainder after division. It is extremely useful for checking if a number is even or odd, or for cycling through values.

# Check if a number is even or odd
num <- 17
remainder <- num %% 2
cat(num, 'is', ifelse(remainder == 0, 'even', 'odd'), '
')

# Cycle through hours (0-23)
minutes <- 150
hours <- minutes %% 60
cat('150 minutes =', hours, 'minutes past the hour
')

Integer Division %/%

The %/% operator performs integer (floor) division, returning only the whole-number quotient and discarding the remainder. Think of it as how many whole times one number fits into another.

total_items <- 23
box_size <- 5
full_boxes <- total_items %/% box_size
leftover <- total_items %% box_size
cat('Full boxes:', full_boxes, '
')
cat('Leftover items:', leftover, '
')
cat('Check: 7 %/% 2 =', 7 %/% 2, '
')

Equality and Inequality

Comparison operators return TRUE or FALSE. Use == to test equality and != to test inequality. Note: == is different from = (assignment).

score <- 85
passing_grade <- 60

cat('Did student pass?', score >= passing_grade, '
')
cat('Is score exactly 100?', score == 100, '
')
cat('Score is not 85?', score != 85, '
')

Less Than and Greater Than

Use <, >, <=, and >= for ordering comparisons. These work on numbers, strings (alphabetical), and even dates.

temp_c <- 37.2
normal_temp <- 37.0

cat('Fever?', temp_c > normal_temp, '
')
cat('Hypothermia?', temp_c < 35.0, '
')
cat('Normal or above?', temp_c >= normal_temp, '
')

# Works on strings too
cat('apple < banana:', 'apple' < 'banana', '
')

Comparing Vectors

When you apply comparison operators to vectors, R compares element by element and returns a logical vector. This is called vectorisation and is a core R feature.

ages <- c(12, 25, 17, 34, 8)
cat('Ages:', ages, '
')
cat('Over 18:', ages > 18, '
')
cat('Exactly 17:', ages == 17, '
')

# Count how many are adults
cat('Number of adults:', sum(ages >= 18), '
')

Combining Arithmetic and Comparison

You can combine arithmetic and comparison operators in a single expression. R evaluates arithmetic first, then comparisons. This lets you write concise conditional checks.

# BMI check
weight_kg <- 70
height_m <- 1.75
bmi <- weight_kg / (height_m ^ 2)
cat('BMI:', round(bmi, 1), '
')
cat('Healthy BMI (18.5-24.9)?', bmi >= 18.5 & bmi <= 24.9, '
')

# Discount eligibility
total <- 120
cat('Qualifies for discount?', total >= 100, '
')

Operator Summary Table

Here is a quick reference for all arithmetic and comparison operators in R:

  • + — addition
  • - — subtraction
  • * — multiplication
  • / — division (always returns double)
  • ^ — exponentiation
  • %% — modulo (remainder)
  • %/% — integer division
  • ==, != — equality, inequality
  • <, >, <=, >= — ordering
# Quick demo of all operators
x <- 10
y <- 3
cat('10 + 3 =', x + y, '
')
cat('10 - 3 =', x - y, '
')
cat('10 * 3 =', x * y, '
')
cat('10 / 3 =', x / y, '
')
cat('10 %% 3 =', x %% y, '
')
cat('10 %/% 3 =', x %/% y, '
')
cat('10 > 3 =', x > y, '
')

Quick Check

What does the expression 17 %% 5 evaluate to in R?

Recap: Operators and Expressions

Great work! Here are the key takeaways from this lesson:

  • R provides arithmetic operators: +, -, *, /, ^, %%, %/%
  • %% gives the remainder; %/% gives the whole quotient
  • Comparison operators (==, !=, <, >, etc.) return TRUE or FALSE
  • Operators are vectorised — they apply element-by-element to vectors
  • Arithmetic is evaluated before comparisons (standard precedence)
# Final example: find numbers divisible by 3
nums <- 1:15
divisible_by_3 <- nums[nums %% 3 == 0]
cat('Numbers 1-15 divisible by 3:', divisible_by_3, '
')
Free to start

Learn R with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
43
Lessons
159

Frequently Asked Questions

Is the “Arithmetic and Comparison Operators” lesson free?

Yes — any 3 lessons of the R Academy learning path, including “Arithmetic and Comparison Operators”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The R Academy course includes 4 lessons in total.

What will I learn in “Arithmetic and Comparison Operators”?

Learn +, -, *, /, %%, %/% and comparison operators in R. You practise R Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start R Academy?

No prior experience is required. R Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Arithmetic and Comparison Operators” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this R Academy lesson?

Yes. Every R Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arithmetic and Comparison Operators
  2. Logical Operators and Boolean Logic
  3. Assignment and Special Operators
  4. Operator Precedence and Complex Expressions
← Back to R Academy