Operator Precedence


Operator Precedence

When multiple operators are used in a single expression, we need to know the precedence of these operators to figure out the sequence of operation that will take place.

Precedence defines the order of execution, i.e., which operator gets the higher priority.


Example 1: Operator Precedence in R

> 2 + 6 * 5
[1] 32

Here, the * operator gets higher priority than + and hence 2 + 6 * 5 is interpreted as 2 + (6 * 5). This order can be changed with the use of parentheses ().

> (2 + 6) * 5
[1] 40

Operator Associativity

It is possible to have multiple operators of same precedence in an expression. In such case the order of execution is determined through associativity.

The associativity of operators is given in the table above.

We can see that most of them have left to right associativity.


Example 2: Operator Associativity in R

> 3 / 4 / 5
[1] 0.15

In the above example, 3 / 4 / 5 is evaluated as (3 / 4) / 5 due to left to right associativity of the / operator. However, this order too can be changed using parentheses ().

> 3 / (4 / 5)
[1] 3.75

Precedence and Associativity of different operators in R from highest to lowest

Operator Precedence in R
OperatorDescriptionAssociativity
^ExponentRight to Left
-x, +xUnary minus, Unary plusLeft to Right
%%ModulusLeft to Right
*, /Multiplication, DivisionLeft to Right
+, –Addition, SubtractionLeft to Right
<, >, <=, >=, ==, !=ComparisionsLeft to Right
!Logical NOTLeft to Right
&, &&Logical ANDLeft to Right
|, ||Logical ORLeft to Right
->, ->>Rightward assignmentLeft to Right
<-, <<-Leftward assignmentRight to Left
=Leftward assignmentRight to Left

Comments

Popular posts from this blog

spreadsheet in excel

📚Gds-Tech 📚 EMS

vocab