01 / 20
> + * ^

Arithmetic Operators
& Precedence in R

Master how R evaluates mathematical expressions using operators and the order of operations that determines every result.

R Programming

What Are Arithmetic Operators?

Arithmetic operators are symbols that tell R to perform a mathematical calculation on one or more values. They are the most fundamental building blocks of any R expression.

+  Addition
Adds two values: \(3 + 5\) gives \(8\)
-  Subtraction
Subtracts: \(10 - 4\) gives \(6\)
*  Multiplication
Multiplies: \(7 * 3\) gives \(21\)

Division, Exponents, & Modulo

/  Division
Divides: \(15 / 4\) gives \(3.75\)
^  Exponent
Power: \(2^5\) gives \(32\)
%%  Modulo
Remainder: \(17 \mathbin{\%%} 5\) gives \(2\)
%/%  Integer Division
Quotient: \(17 \mathbin{\%/\%} 5\) gives \(3\)

Why Does Precedence Matter?

When an expression contains multiple operators, R must decide which to evaluate first. The result can change dramatically depending on the order.

# Same numbers, very different answers 2 + 3 * 4 # [1] 14 (multiplication first) (2 + 3) * 4 # [1] 20 (parentheses override)

R's Precedence Hierarchy

1. Parentheses   \(()\)
HIGHEST
2. Exponentiation   \(^{\wedge}\)
3. Unary minus / plus   \(-x, +x\)
4. Multiply, Divide, Modulo & Integer Div   \(*, /, %%, %/\%\)
5. Add & Subtract   \(+, -\)
LOWEST

Complete Precedence Table

Priority Operator Description Associativity
1 (highest) \(()\) Parentheses Inner → outer
2 \(^{\wedge}\) Exponentiation Right → left
3 \(-x, +x\) Unary minus/plus Right → left
4 \(*, /, %%, %/\%\) Multiply, divide, modulo, integer div Left → right
5 (lowest) \(+, -\) Add, subtract Left → right

Right vs Left Associativity

When two operators have the same precedence, associativity determines the direction R reads them. Most operators go left to right, but exponentiation is different.

Left → Right
\(10 - 3 - 2\) evaluates as \((10 - 3) - 2 = 5\). R works from the left side first. Applies to \(+, -, *, /, %%, %/\%\).
Right → Left
\(2^3^2\) evaluates as \(2^{(3^2)} = 2^9 = 512\). R works from the right side first. Applies to \(^{\wedge}\) and unary \(-\).

Parentheses Override Everything

Parentheses have the highest priority. R always evaluates the innermost parentheses first, then works outward. You can use them to force any evaluation order.

# Without parentheses 10 + 6 / 2 # [1] 13 (division first: 10 + 3) # With parentheses (10 + 6) / 2 # [1] 8 (addition first: 16 / 2) # Nested parentheses ((2 + 3) * (8 - 2)) ^ 2 # [1] 900 (5 * 6 = 30, then 30^2)

How to Evaluate Any Expression

1
Find the innermost parentheses. Evaluate everything inside them first. Repeat for nested groups.
2
Apply exponentiation (\(^\)). Remember: right-to-left if multiple exponents are chained.
3
Apply unary minus/plus. Negate or affirm values (e.g., \(-x\) becomes negative).
4
Apply \(*\), \(/\), \(%%\), and \(\%/\%\). These all share the same precedence level, so evaluate them from left-to-right.
5
Apply \(+\) and \(-\) last. Work left-to-right. The final result is your answer.

Example 1: Step-by-Step

Evaluate:   \(3 + 4 * 2^2 - 1\)

1
Exponent first: \(2^2 = \) 4 → expression becomes \(3 + 4 * 4 - 1\)
2
Multiplication next: \(4 * 4 = \) 16 → expression becomes \(3 + 16 - 1\)
3
Add and subtract left-to-right: \(3 + 16 = 19\), then \(19 - 1 = \) 18
3 + 4 * 2^2 - 1 # [1] 18

Example 2: Parentheses Change Results

Evaluate:   \((8 + 2) * (9 - 4) / 2\)

1
First parentheses: \(8 + 2 = \) 10
2
Second parentheses: \(9 - 4 = \) 5
3
Multiply and divide left-to-right: \(10 * 5 = 50\), then \(50 / 2 = \) 25
(8 + 2) * (9 - 4) / 2 # [1] 25

Example 3: Modulo & Integer Division

Evaluate:   \(20 \mathbin{\%%} 3 + 20 \mathbin{\%/\%} 3 * 2\)

1
High Precedence (Left-to-Right): The operators \(%%\), \(\%/\%\), and \(*\) are all evaluated before \(+\). They are handled from left to right.
2
Step 1: \(20 \mathbin{\%%} 3\) evaluates to 2. The expression becomes \(2 + 20 \mathbin{\%/\%} 3 * 2\).
3
Step 2: \(20 \mathbin{\%/\%} 3\) evaluates to 6. The expression becomes \(2 + 6 * 2\).
4
Step 3: \(6 * 2\) evaluates to 12. The expression becomes \(2 + 12\).
5
Step 4: \(2 + 12\) evaluates to 14.
20 %% 3 + 20 %/% 3 * 2 # [1] 14

The Unary Minus Surprise

In R, the unary minus (negation) has lower precedence than exponentiation. This means \(-2^2\) might not give the answer you expect.

# Surprise! Unary minus applies AFTER ^ -2^2 # [1] -4 (computes -(2^2), not (-2)^2) # To square negative 2, use parentheses (-2)^2 # [1] 4 (computes (-2)^2)

Common Mistakes to Avoid

✕ Left-to-Right Fallacy
Assuming R reads all operators left-to-right. Exponentiation is right-to-left: \(2^3^2 = 512\), not \(64\).
✕ Forgetting Unary Minus
Writing \(-x^2\) and expecting the result of \((-x)^2\). Due to precedence, R computes \(-(x^2)\) instead. Always use parentheses like \((-x)^2\) to square a negative number.
✕ Confusing %% and %/%
Using \(%%\) when you want the quotient, or \(\%/\%\) when you want the remainder. Mix-ups silently return wrong answers.

Quick Reference Summary

Operator R Syntax Example Result
Addition \(+\) \(7 + 3\) 10
Subtraction \(-\) \(7 - 3\) 4
Multiplication \(*\) \(7 * 3\) 21
Division \(/\) \(7 / 2\) 3.5
Exponent \(^{\wedge}\) \(2^5\) 32
Modulo \(%%\) \(17 \mathbin{\%%} 5\) 2
Integer Div \(\%/\%\) \(17 \mathbin{\%/\%} 5\) 3

Question 1 of 5

What is the result of this R expression?
\(5 + 3 * 2\)

Question 2 of 5

What does -3^2 return in R?

Question 3 of 5

What is the result of 17 %% 5 in R?

Question 4 of 5

What does 2^3^2 evaluate to in R?

Question 5 of 5

What is the result of (4 + 6) %/% 3 + 2 * 5?