Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

R Operator Precedence


Share

Operator Precedence

When a calculation has more than one operator, R follows rules to decide which part to calculate first, just like in math class.

For example, multiplication happens before addition:

Example

2 + 3 * 4
(2 + 3) * 4
Try it Yourself »

In 2 + 3 * 4, R calculates 3 * 4 first, so the answer is 14.

If you want the addition to happen first, use parentheses: (2 + 3) * 4 gives 20.

Tip: Use parentheses ( ) whenever you want to be sure about the order. It also makes your code easier to read.


Order of Operations

Here are the most common operators in R, from highest to lowest priority:

Operator Description
( ) Parentheses
^ Exponent
-x Minus sign in front of a value
: Sequence
%% %/% %in% Modulus, integer division and %in%
* / Multiplication and division
+ - Addition and subtraction
== != < > <= >= Comparison
! NOT
& && AND
| || OR
<- = Assignment


Left to Right

Operators with the same priority, like + and -, are calculated from left to right:

Example

10 - 2 + 5
10 - (2 + 5)
Try it Yourself »

10 - 2 + 5 is calculated as (10 - 2) + 5, which is 13. With parentheses, 10 - (2 + 5) is 10 - 7, which is 3.


Watch Out for Minus

The exponent (^) is calculated before the minus sign in front of a number. So -2 ^ 2 means -(2 ^ 2):

Example

-2 ^ 2
(-2) ^ 2
Try it Yourself »

If you want to square -2, put it in parentheses: (-2) ^ 2 gives 4.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.