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
- Arithmetic operators including %% (modulo) and ^ (power)
- Comparison operators and why equality uses ==
- How comparisons return TRUE/FALSE across whole vectors
- Logical operators & (AND), | (OR), and ! (NOT)
- The difference between & and && in R
- Membership testing with the %in% operator
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] 82️⃣ 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 TRUEThat 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] TRUEYour 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] TRUEMini-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)
- Using = instead of == — x = 5 assigns; to test equality write x == 5.
- Comparing floating-point numbers with == — 0.1 + 0.2 == 0.3 is FALSE due to rounding. Use all.equal() for near-equality.
- Wrong AND/OR for vectors — use single & / | for element-wise vector logic; && / || only look at the first element.
- Forgetting %in% is one-directional — it checks each left value against the whole right set, not the reverse.
Pro Tips
- 💡 Even/odd test: n %% 2 == 0 is TRUE for even numbers.
- 💡 Negate a set test: !(x %in% bad) keeps everything not in a list.
- 💡 Operator precedence: when mixing & and |, add parentheses to make intent explicit and avoid surprises.
📋 Quick Reference — Operators
| Meaning | Operator |
|---|---|
| Remainder / int division | %% , %/% |
| Power | 2 ^ 3 |
| Equal / not equal | == , != |
| AND / OR / NOT | & , | , ! |
| Membership | x %in% set |
| Near equality | all.equal(a, b) |
🎉 Lesson Complete!
- ✅ Arithmetic includes %%, %/%, and ^
- ✅ Equality is ==; assignment is <-
- ✅ Comparisons return TRUE/FALSE across whole vectors
- ✅ Combine conditions with &, |, and !
- ✅ %in% tests set membership cleanly
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
- Previous: Vectors
- Next: Factors — Represent categorical data with levels — essential for stats and plots
- Quick reference: R cheat sheet
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.