Operators

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.

Learn Operators in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, 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.

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.

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.

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

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

📋 Quick Reference — Operators

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.