Skip to main content
R Programming
CHAPTER 05 Beginner

Operators and Expressions

Updated: May 18, 2026
5 min read

# CHAPTER 5

Operators and Expressions in R

1. Chapter Introduction

Operators are the building blocks of every R expression — from simple arithmetic to complex data filtering conditions. This chapter covers R's complete operator system with data analysis use cases.

2. Arithmetic Operators

r
123456789101112131415161718192021
# All arithmetic operators
x <- 17
y <- 5

x + y    # 22  — Addition
x - y    # 12  — Subtraction
x * y    # 85  — Multiplication
x / y    # 3.4 — Division
x %/% y  # 3   — Integer division (floor)
x %% y   # 2   — Modulo (remainder)
x ^ y    # 1419857 — Exponentiation
x ** y   # 1419857 — Alternative (less common)

# Vectorized arithmetic (R's superpower)
scores <- c(75, 82, 91, 68, 55, 88, 79)
# Scale all scores to percentage: no loop needed!
percentages <- scores / 100
weighted    <- scores * 0.4   # 40% weight
bonus       <- scores + 5     # Add 5 points to everyone
squared     <- scores ^ 2
print(round(percentages, 2))  # [1] 0.75 0.82 0.91 0.68 0.55 0.88 0.79

3. Comparison Operators

r
1234567891011121314151617181920212223
# Comparison operators (return logical TRUE/FALSE)
5 > 3    # TRUE
5 < 3    # FALSE
5 >= 5   # TRUE
5 <= 4   # FALSE
5 == 5   # TRUE   (equality)
5 != 3   # TRUE   (not equal)

# Vectorized comparison — essential for data filtering
ages <- c(15, 22, 17, 35, 28, 16, 42)
ages >= 18          # FALSE TRUE FALSE TRUE TRUE FALSE TRUE
ages[ages >= 18]    # 22 35 28 42  ← filter adults

# NEVER use == for floating point comparison
0.1 + 0.2 == 0.3   # FALSE! (floating point precision issue)
all.equal(0.1 + 0.2, 0.3)  # TRUE ← correct way
abs(0.1 + 0.2 - 0.3) < 1e-10  # TRUE ← alternative

# %in% — membership test
fruit <- c("apple", "mango", "banana", "orange")
"mango" %in% fruit    # TRUE
"grape" %in% fruit    # FALSE
c("apple", "grape") %in% fruit  # TRUE FALSE

4. Logical Operators

r
12345678910111213141516171819202122232425
# Scalar logical operators
TRUE & FALSE   # FALSE  (AND)
TRUE | FALSE   # TRUE   (OR)
!TRUE          # FALSE  (NOT)
xor(TRUE, FALSE)  # TRUE  (exclusive OR)

# Vectorized logical operators (element-wise, for vectors)
& — AND (vectorized)
| — OR  (vectorized)

# Short-circuit operators (for scalars — if/while conditions)
&& — AND (short-circuit: stops at first FALSE)
|| — OR  (short-circuit: stops at first TRUE)

# Example: filtering data
salary <- c(45000, 72000, 88000, 55000, 95000, 62000)
dept   <- c("HR", "IT", "IT", "Finance", "IT", "HR")
# AND: IT department with salary > 70000
high_it <- dept == "IT" & salary > 70000
salary[high_it]   # 72000 88000 95000

# any() and all()
any(salary > 90000)   # TRUE (at least one)
all(salary > 40000)   # TRUE (all satisfy)
any(salary > 150000)  # FALSE

5. Assignment and Special Operators

r
12345678910111213141516171819202122232425262728293031
# Assignment operators
x  <- 10      # Left assignment (standard)
10 -> y       # Right assignment (rare, avoid)
x  =  10      # Also valid but non-conventional in scripts
x  <<- 10     # Global assignment (from inside functions)

# Pipe operators (modern R workflows)
# Base R pipe (R 4.1+)
c(1:10) |> mean()    # 5.5

# Magrittr pipe (tidyverse — more features)
library(magrittr)
c(1:10) %>% mean()   # 5.5

# Pipe chain example
library(dplyr)
mtcars %>%
  filter(cyl == 6) %>%
  select(mpg, hp, wt) %>%
  summarise(avg_mpg = mean(mpg))

# Colon operator (sequence)
1:10         # 1 2 3 4 5 6 7 8 9 10
5:1          # 5 4 3 2 1
seq(1, 10, by=2)  # 1 3 5 7 9

# :: namespace operator
dplyr::filter(mtcars, cyl == 6)  # Explicitly use dplyr's filter

# Tilde ~ formula operator (for models and plots)
lm(mpg ~ hp + wt, data = mtcars)  # linear model formula

6. Operator Precedence

text
12345678910111213141516
R Operator Precedence (highest to lowest):
  1. ::, :::   (namespace)
  2. $, @      (list/slot access)
  3. [, [[, ]  (indexing)
  4. ^         (exponentiation — right to left)
  5. -, +      (unary)
  6. :         (sequence)
  7. %any%     (special ops like %%,  %in%, %*%)
  8. *, /      (multiply, divide)
  9. +, -      (add, subtract)
  10. <, >, <=, >= (comparison)
  11. ==, !=   (equality)
  12. !        (logical NOT)
  13. &, &&    (logical AND)
  14. |, ||    (logical OR)
  15. <-, =, ->  (assignment)

7. Common Mistakes

  • Using & vs && for if conditions: & is vectorized (returns vector); && is scalar (returns one TRUE/FALSE). Always use && and || in if() conditions.
  • == to compare with NA: x == NA is always NA. Use is.na(x).

8. MCQs

Question 1

17 %% 5 in R returns?

Question 2

%in% operator checks?

Question 3

&& vs & difference?

Question 4

Floating point comparison should use?

Question 5

<<- operator does?

Question 6

!TRUE evaluates to?

Question 7

1:5 creates?

Question 8

xor(TRUE, FALSE) returns?

Question 9

dplyr::filter() uses :: to?

Question 10

|> in R 4.1+ is?

9. Interview Questions

  • Q: What is the difference between & and && in R?
  • Q: Why does 0.1 + 0.2 == 0.3 return FALSE in R?

10. Summary

R operators: arithmetic (+, -, *, /, %/%, %%, ^), comparison (<, >, ==, !=, %in%), logical (&, |, !, &&, ||), assignment (<-, <<-), pipe (|>, %>%), sequence (:). Key rules: use &&/|| in if(), never == for NA/floats, %in% for membership, :: for explicit namespace. R's vectorized operators make filtering and transformation loop-free.

11. Next Chapter Recommendation

In Chapter 6: Conditional Statements and Loops, we control program flow with if/else, for, while loops, and build a grade evaluation system.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·