ZetCode

R operators

last modified November 19, 2026

An operator is a symbol that combines values, and each value it takes is an operand. R groups its operators into arithmetic, comparison, logical, and special operators. Most of them are vectorised, so they work on whole vectors without a loop. This article covers the operators, the rules that decide which one runs first, and the special values that arithmetic can produce.

Arithmetic operators

The five arithmetic operators are addition, subtraction, multiplication, division, and exponentiation. The first four use the familiar symbols, and exponentiation uses the caret. Division always produces a double, even when both operands are integers and the result is a whole number.

arith.R
cat("7 + 3 =", 7 + 3, "\n")
cat("7 - 3 =", 7 - 3, "\n")
cat("7 * 3 =", 7 * 3, "\n")
cat("7 / 3 =", 7 / 3, "\n")
cat("7 ^ 3 =", 7 ^ 3, "\n")

The results are ordinary numbers, printed with seven significant digits by default. The multiplication sign is the asterisk, as in most programming languages. The caret is the only operator in this group that is right-associative, a property that matters in the precedence section below.

$ Rscript arith.R
7 + 3 = 10 
7 - 3 = 4 
7 * 3 = 21 
7 / 3 = 2.333333 
7 ^ 3 = 343 

Integer division and remainder

Two operators split a division into a whole part and a remainder. %/% performs integer division: it divides and rounds down toward negative infinity. %% returns the remainder, also called the modulo. Together they satisfy the identity a == (a %/% b) * b + a %% b for every non-zero b.

intdiv.R
cat("7 %/% 3 =", 7 %/% 3, "\n")
cat("7 %% 3 =", 7 %% 3, "\n")
cat("-7 %/% 3 =", -7 %/% 3, "\n")
cat("-7 %% 3 =", -7 %% 3, "\n")
cat("7 %% -3 =", 7 %% -3, "\n")

For positive numbers the result is the one children learn in school, but the negative cases show the rule clearly. -7 %/% 3 is -3, not -2, because the quotient is rounded down, and the remainder is then 2. The last line shows that the remainder takes the sign of the divisor, so 7 %% -3 is -2 rather than 1.

$ Rscript intdiv.R
7 %/% 3 = 2 
7 %% 3 = 1 
-7 %/% 3 = -3 
-7 %% 3 = 2 
7 %% -3 = -2 

Comparison operators

The comparison operators are == for equality, != for inequality, and <, <=, >, and >= for order. They return logical values, and applied to a vector they return one logical value per element. The single equals sign is assignment and cannot stand in for ==.

compare.R
cat("5 > 3:", 5 > 3, "\n")
cat("5 == 5:", 5 == 5, "\n")
cat("5 != 5:", 5 != 5, "\n")
cat('"a" < "b":', "a" < "b", "\n")
cat("scores > 70:", c(72, 85, 60) > 70, "\n")
print(NA == 5)

Strings compare in alphabetical order, following the rules of the current locale. A comparison with a missing value is not false; it is unknown, so NA == 5 returns NA. Use is.na to test for missing values, and remember that a condition built from a comparison can be NA as well as true or false.

$ Rscript compare.R
5 > 3: TRUE 
5 == 5: TRUE 
5 != 5: FALSE 
"a" < "b": TRUE 
scores > 70: TRUE TRUE FALSE 
[1] NA

Logical operators

The logical operators combine logical values. & is and, | is or, and ! is not. All three are vectorised, so they combine two vectors element by element. The function xor performs the exclusive or, which is true when exactly one operand is true.

logical-ops.R
a <- c(TRUE, TRUE, FALSE, FALSE)
b <- c(TRUE, FALSE, TRUE, FALSE)
print(a & b)
print(a | b)
print(!a)
print(xor(a, b))
print(NA & FALSE)
print(NA | TRUE)
print(NA & TRUE)

The first four results are the truth tables applied to four pairs. The last three lines show how NA behaves: FALSE makes an and expression false regardless of the unknown value, and TRUE makes an or expression true. Only when the unknown value can change the result does the result stay NA. This three-valued logic is the reason NA must be handled explicitly rather than treated as false.

$ Rscript logical-ops.R
[1]  TRUE FALSE FALSE FALSE
[1]  TRUE  TRUE  TRUE FALSE
[1] FALSE FALSE  TRUE  TRUE
[1] FALSE  TRUE  TRUE FALSE
[1] FALSE
[1] TRUE
[1] NA

Short-circuit operators

The double forms && and || work on single logical values rather than on vectors, and they stop as soon as the answer is known. That behaviour is called short-circuit evaluation. If the left operand of && is false, the right operand is not evaluated at all; if the left operand of || is true, the right operand is skipped too.

scalar.R
print(FALSE && print("evaluated"))
print(TRUE || print("evaluated"))
cat("TRUE && FALSE:", TRUE && FALSE, "\n")
cat("TRUE || FALSE:", TRUE || FALSE, "\n")
cat("!TRUE:", !TRUE, "\n")
cat("isTRUE(1 == 1):", isTRUE(1 == 1), "\n")

The word evaluated never appears in the output, because neither right operand was reached. This is what makes && safe in a condition such as n > 0 && total / n > 10: the division is protected by the first test. Because the operators expect a single value, they are not a replacement for & and | on vectors. isTRUE tests that a value is exactly one TRUE, which avoids an NA result.

$ Rscript scalar.R
[1] FALSE
[1] TRUE
TRUE && FALSE: FALSE 
TRUE || FALSE: TRUE 
!TRUE: FALSE 
isTRUE(1 == 1): TRUE 

Membership

The operator %in% tests membership in a set. It returns a logical vector with one value per element of its left operand, and a value is true when it appears anywhere in the right operand. Unlike ==, it compares against a whole collection at once and never recycles the collection.

membership.R
print(3 %in% c(1, 2, 3))
print("b" %in% c("a", "b"))
print(c(1, 5, 3) %in% c(1, 2, 3))

The results are true, true, and the vector TRUE FALSE TRUE. The operator is useful for filtering rows or keeping only the names that belong to a list. Negate it with !, as in !x %in% y, which removes the members of y from x.

$ Rscript membership.R
[1] TRUE
[1] TRUE
[1]  TRUE FALSE  TRUE

Sequences and the colon

The colon operator builds a sequence of numbers from its left operand to its right operand in steps of one or minus one, so 1:5 counts up and 5:1 counts down. The operator binds more tightly than multiplication, which means that 1:5 * 2 doubles the whole sequence rather than moving the end point.

colon-sequences.R
print(1:5)
print(5:1)
print(1:10)
print(1:5 * 2)

The last line produces 2, 4, 6, 8, and 10. A non-integer bound is kept as it is, so 1.5:3 produces 1.5 and 2.5 and stops before the bound. A sequence that would be empty counts down instead, so 1:0 returns the two values 1 and 0. The function seq, covered with vectors, builds sequences with a step and a length.

$ Rscript colon-sequences.R
[1] 1 2 3 4 5
[1] 5 4 3 2 1
 [1]  1  2  3  4  5  6  7  8  9 10
[1]  2  4  6  8 10

Special values in arithmetic

Division by zero follows the IEEE 754 rules rather than raising an error. A positive number divided by zero gives positive infinity, a negative number gives negative infinity, and zero divided by zero gives not a number. Missing values propagate through arithmetic, and so does the undefined result of infinity minus infinity.

special.R
cat("1 / 0 =", 1 / 0, "\n")
cat("0 / 0 =", 0 / 0, "\n")
cat("NA + 1 =", NA + 1, "\n")
cat("Inf - Inf =", Inf - Inf, "\n")

The output prints Inf, NaN, NA, and NaN. Infinity is a usable number in comparisons and limits, while NaN and NA mark results that cannot be used without handling. The data types article explains how is.finite, is.nan, and is.na distinguish them.

$ Rscript special.R
1 / 0 = Inf 
0 / 0 = NaN 
NA + 1 = NA 
Inf - Inf = NaN 

Operator precedence

When one expression contains several operators, precedence decides which one runs first, and associativity decides what happens when two operators of the same precedence meet. Operators written between percent signs, such as %in%, are called special operators and form their own precedence group. Most R operators are left-associative, but exponentiation and assignment are right-associative. The table lists the groups from the tightest to the loosest binding.

OperatorsMeaning
^exponentiation
-, + (unary)sign
:sequence
%...%special operators
*, /multiplication, division
+, - (binary)addition, subtraction
<, >, <=, >=, ==, !=comparison
!negation
&, &&and
|, ||or
->, ->>right assignment
<-, <<-left assignment
=assignment

The unary minus comes after the caret, so -2 ^ 2 is -(2 ^ 2) and equals -4. The caret is right-associative, so 2 ^ 3 ^ 2 is 2 ^ (3 ^ 2) and equals 512. The special operators, which include %/% and %%, bind more tightly than multiplication even though they look like division. Negation binds more loosely than comparison, which makes !1 == 2 mean !(1 == 2) and return true, a result that surprises many readers.

precedence.R
cat("2 + 3 * 4 =", 2 + 3 * 4, "\n")
cat("(2 + 3) * 4 =", (2 + 3) * 4, "\n")
cat("2 ^ 3 ^ 2 =", 2 ^ 3 ^ 2, "\n")
cat("-2 ^ 2 =", -2 ^ 2, "\n")
cat("2 + 3 > 4 =", 2 + 3 > 4, "\n")
cat("!1 == 2 =", !1 == 2, "\n")

The first pair shows why parentheses matter even when the default is what you want: the reader should not have to remember the table. The last two lines show the less obvious rules, where addition happens before comparison and negation happens after it. When in doubt, add parentheses; they cost nothing and remove the ambiguity.

$ Rscript precedence.R
2 + 3 * 4 = 14 
(2 + 3) * 4 = 20 
2 ^ 3 ^ 2 = 512 
-2 ^ 2 = -4 
2 + 3 > 4 = TRUE 
!1 == 2 = TRUE 

A complete example

The next script puts the arithmetic operators to work. It computes the value of an investment after ten years of compound interest, using the formula principal * (1 + rate) ^ years, and then subtracts the principal to get the gain. The function round keeps two decimal places for the printed result.

interest.R
principal <- 1000
rate <- 0.05
years <- 10
amount <- principal * (1 + rate) ^ years
gain <- amount - principal
cat("amount:", round(amount, 2), "\n")
cat("gain:", round(gain, 2), "\n")

The parentheses around 1 + rate are required: without them the caret would apply only to rate, and the result would be principal * 1 + rate ^ years. The amount is 1628.89 and the gain is 628.89. The same pattern appears in loan payments, population growth, and any other calculation that repeats a percentage change.

$ Rscript interest.R
amount: 1628.89 
gain: 628.89 

Other operators

R has a few more operators that are introduced with the structures they act on. %*% performs matrix multiplication and %o% builds an outer product, both covered in the matrices article. The native pipe |> passes a value into a function and is covered with data manipulation, while the dollar sign, the double bracket, and the single bracket are indexing operators treated in the vectors and lists articles.

Next steps

The control flow article uses the comparison and logical operators in conditions, and the vectors article later in the series shows how the vectorised operators build and transform whole vectors. The data types article explains the values that the operators return and the special values they can produce.

Source

The R Language Definition: operators

R documentation: operator precedence

R documentation: arithmetic operators

R documentation: match and %in%

In this article we have used the arithmetic operators, split division into a quotient and a remainder, compared values, and combined logical vectors with &, |, and !. We saw the difference between vectorised and short-circuit operators, tested membership with %in%, built sequences with the colon, and read the precedence table. The series continues with R control flow.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all R Language tutorials.