Operators

Reviewed & published by Brayan K

Operators are the symbols R uses to do arithmetic, compare values, and combine logical conditions — and in R they all work on whole vectors at once.

Part of the free R course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.

By the end of this lesson you'll use arithmetic operators (including modulo and exponent), compare values with the right operators, and combine conditions with AND, OR, NOT, and membership tests.

What You'll Learn in This Lesson

1️⃣ Arithmetic Operators

Beyond the usual + - * /, R adds %% for the remainder (modulo), %/% for integer division, and ^ for powers. All of them are vectorized.

# Arithmetic operators work on numbers and whole vectors alike.

7 + 3                # addition       -> [1] 10
7 - 3                # subtraction    -> [1] 4
7 * 3                # multiplication -> [1] 21
7 / 3                # division       -> [1] 2.333333
7 %% 3               # modulo (remainder) -> [1] 1
7 %/% 3              # integer division   -> [1] 2
2 ^ 3                # exponent        -> [1] 8

2️⃣ Comparison Operators

Comparisons ask a question and answer with TRUE or FALSE. Note that equality is == (double equals) — a single = means assignment. Because comparisons are vectorized, you get a TRUE/FALSE for every element.

# Comparison operators return TRUE/FALSE (logical values).

5 > 3                # -> [1] TRUE
5 < 3                # -> [1] FALSE
5 == 5               # equal? (note the DOUBLE =)  -> [1] TRUE
5 != 3               # not equal?                  -> [1] TRUE
5 >= 5               # -> [1] TRUE

# Comparisons are vectorized too:
c(1, 5, 9) > 4       # -> [1] FALSE  TRUE  TRUE

That vector of TRUE/FALSE is exactly what powered the logical indexing you saw in the Vectors lesson — x[x > 4] keeps the elements where the test is TRUE.

3️⃣ Logical Operators

To combine conditions, use & (AND), | (OR), and ! (NOT). The handy %in% operator tests whether a value belongs to a set, which is much cleaner than chaining many == checks.

# Logical operators combine TRUE/FALSE values.
# &  = AND      |  = OR      ! = NOT

age <- 20
income <- 35000

age >= 18 & income > 30000    # both must be true -> [1] TRUE
age < 18 | income > 30000     # either is enough  -> [1] TRUE
!(age >= 18)                  # negation          -> [1] FALSE

# %in% tests membership in a set:
"red" %in% c("red", "green", "blue")   # -> [1] TRUE

Your turn. Fill in the ___ blank, run it, and compare with the expected output.

# 🎯 YOUR TURN — replace each ___ then run it.

temp <- 28   # degrees Celsius
humidity <- 70

# 1) Is it "hot AND humid"? (temp above 25 AND humidity above 60)
hot_humid <- temp > 25 ___ humidity > 60   # 👉 replace ___ with &
print(hot_humid)

# 2) Is the remainder of 28 divided by 5 equal to 3?
print(temp %% 5 == 3)

# ✅ Expected output:
# [1] TRUE
# [1] TRUE

Mini-Challenge: Eligibility Check

Write it from the outline, run it, and check it against the example output. This combines comparison, %in%, and & — the everyday toolkit for filtering.

Common Errors (and the fix)

Pro Tips

📋 Quick Reference — Operators

MeaningOperator
Remainder / int division%% , %/%
Power2 ^ 3
Equal / not equal== , !=
AND / OR / NOT& , | , !
Membershipx %in% set
Near equalityall.equal(a, b)

🎉 Lesson Complete!

Practice quiz

What does 7 %% 3 return?

  • 1
  • 2
  • 21
  • 2.333

Answer: 1. %% is modulo, the remainder after division: 7 divided by 3 leaves 1.

What does 7 %/% 3 return?

  • 1
  • 2
  • 3
  • 2.333

Answer: 2. %/% is integer division: 3 goes into 7 two whole times.

Which operator tests equality in R?

  • =
  • ===
  • ==
  • :=

Answer: ==. Double == tests equality; a single = means assignment.

What does 2 ^ 3 evaluate to?

  • 6
  • 5
  • 9
  • 8

Answer: 8. ^ is the exponent operator, so 2 to the power 3 is 8.

Which operator means logical AND for vectors?

  • &
  • &&
  • +
  • and

Answer: &. Single & is the vectorized AND; && only checks the first element.

What does "red" %in% c("red", "green", "blue") return?

  • FALSE
  • TRUE
  • NA
  • "red"

Answer: TRUE. %in% tests membership; "red" is in the set, so it returns TRUE.

How do you negate a condition in R?

  • not()
  • ~
  • ! before it
  • -

Answer: ! before it. The ! operator is logical NOT, e.g. !(age >= 18).

Why is 0.1 + 0.2 == 0.3 FALSE in R?

  • == is broken
  • 0.3 is a string
  • Floating-point rounding error
  • Order matters

Answer: Floating-point rounding error. Floating-point representation makes them slightly unequal; use all.equal().

What is the difference between & and && in R?

  • & is vectorized; && checks only the first element
  • They are identical
  • && is faster for vectors
  • & is for numbers only

Answer: & is vectorized; && checks only the first element. Single & works element-wise on vectors; && evaluates a single value.

How do you test whether a number n is even?

  • n / 2 == 0
  • n ^ 2 == 0
  • n %/% 2 == 1
  • n %% 2 == 0

Answer: n %% 2 == 0. n %% 2 == 0 is TRUE when the remainder on division by 2 is zero.

Continue this course

Frequently asked questions

Why does R use == for equality instead of =?

A single = means assignment (giving a variable a value), while a double == asks a question: 'are these two things equal?' and returns TRUE or FALSE. Mixing them up is the most common beginner bug. If you write if (x = 5) you'll get an error or unexpected behaviour; you want if (x == 5). Remember: one equals sets, two equals tests.

What is the difference between & and && in R?

The single & (and single |) is vectorized: it compares two vectors element by element and returns a vector of TRUE/FALSE. The double && (and ||) works on a single TRUE/FALSE value and is meant for control flow like if statements. For filtering data you almost always want the single & and |; for an if() condition with one logical value, && is conventional. When in doubt for data work, use the single versions.

What do %% and %/% do?

These are integer-division operators. %% is modulo — the remainder after division, so 7 %% 3 is 1. %/% is integer division — how many whole times one number goes into another, so 7 %/% 3 is 2. They're useful for things like checking if a number is even (n %% 2 == 0) or converting total minutes into hours and minutes.

How does %in% help me?

%in% tests membership: it asks 'is this value one of these?' and returns TRUE or FALSE. It's far cleaner than writing x == "a" | x == "b" | x == "c". It's also vectorized, so c("cat", "fish") %in% c("cat", "dog") returns TRUE FALSE — checking each element on the left against the whole set on the right. You'll use it constantly to filter data to a list of allowed categories.