Operators are the symbols R uses to perform calculations, comparisons, and logical tests. This page covers every operator you'll actually use, with runnable examples.
Arithmetic Operators
These perform standard mathematical operations. Because R is vectorised, they work on entire vectors at once, element by element — no loop required.
R Code
x <- 17; y <- 5 x + y # addition x - y # subtraction x * y # multiplication x / y # division x ^ y # exponentiation x %% y # modulus (remainder) x %/% y # integer (floor) division
Output
[1] 22 [1] 12 [1] 85 [1] 3.4 [1] 1419857 [1] 2 [1] 3
Vectorised Arithmetic
This is one of R's most powerful features: apply one operator to a whole vector at once.
R Code
scores <- c(70, 85, 90, 60) scores + 5 # add 5 bonus marks to everyone scores / 100 * 20 # convert to a mark out of 20
Output
[1] 75 90 95 65 [1] 14 17 18 12
Relational (Comparison) Operators
These compare two values and return TRUE or FALSE.
R Code
a <- 10; b <- 20 a == b # equal to a != b # not equal to a > b # greater than a < b # less than a >= 10 # greater than or equal to a <= 5 # less than or equal to
Output
[1] FALSE [1] TRUE [1] FALSE [1] TRUE [1] TRUE [1] FALSE
Logical Operators
Logical operators combine or invert TRUE/FALSE values.
&and|— element-wise AND / OR, work on whole vectors&&and||— single-value AND / OR, used insideif()conditions!— NOT, flips TRUE to FALSE and vice versa
R Code
x <- c(TRUE, FALSE, TRUE) y <- c(TRUE, TRUE, FALSE) x & y # element-wise AND x | y # element-wise OR !x # NOT 5 > 3 && 2 < 4 # single-value AND, for if() conditions
Output
[1] TRUE FALSE FALSE [1] TRUE TRUE TRUE [1] FALSE TRUE FALSE [1] TRUE
Special Operator: %in%
%in% checks whether each element of one vector exists inside another vector — extremely useful for filtering data.
R Code
fruits <- c("apple", "banana", "mango") "banana" %in% fruits "grape" %in% fruits # works with a whole vector at once c("mango", "grape") %in% fruits
Output
[1] TRUE [1] FALSE [1] TRUE FALSE
Assignment Operators
R Code
x <- 10 # preferred, works everywhere x = 10 # also works, common in other languages 10 -> x # rightward assignment (rare, but valid)
⚠ Common Mistake
Beginners often confuse == (comparison) with = or <- (assignment). Writing if (x = 5) instead of if (x == 5) is one of the most common early bugs.Key Points
- Arithmetic operators (
+ - * / ^ %% %/%) work element-by-element on vectors automatically. - Relational operators (
== != > < >= <=) always return TRUE/FALSE. - Use
&/|for vectors, and&&/||insideif(). %in%is the cleanest way to test membership in a vector.