In this tutorial you will learn about the precedence of operators in R programming language.
Precedence of Operators in R
Precedence of operators in programming language is the order in which the expression is evaluated. When more than one operators are used in an expression, we need to know the order in which the expression will be evaluated. Some operators are given high priority or preference over the other operators. That is some operators will be executed first in the expression regardless of their position in the expression.
In R every operator has given precedence. The following table shows the precedence of operators in R from highest to lowest.
Operators | Meaning |
---|---|
[ , [[ |
indexing |
:: , ::: |
access variables in a name space |
$ |
component extraction |
^ |
exponentiation (right to left) |
- , + |
unary minus and plus |
: |
sequence operator |
%any% |
special operators |
* |
multiply, divide |
+ , - |
(binary) add, subtract |
< , > , <= , >= , == , != |
ordering and comparison |
! |
negation (NOT) |
& , && |
AND |
| , || |
OR |
~ |
as in formula |
-> , ->> |
rightwards assignment |
= |
assignment (right to left) |
<- , <<- |
assignment (right to left) |
? |
help (unary and binary) |
Note that within an expression, operators of equal precedence are evaluated from left to right.
Some Examples of Operator Precedence in R
Example 1: Operator Precedence in R
Consider the example 20 + 3 * 8
. According to the precedence rule, the operator *
gets higher priority than the operator +
. So R will first evaluate the multiplication (*
) 3 * 8
and then use addition (+
) to add 20
to the result of 3 * 8
.
20 + 3 * 8
[1] 44
The order of the operations can be changed by using round parenthesis ( )
.
Consider the arithmetic expression (20 + 3)*8
. According to the precedence rule, R will first evaluate the expression in parenthesis, that is (20 + 3)
and then use multiplication (*
) to multiply the result of (20 +3)
by 8
.
(20 + 3)*8
[1] 184
Note that when there are more parenthesis ()
, then R will evaluate the inner parenthesis first and then evaluate the other parenthesis from inner to outer.
Example 2: Operator Precedence in R
Suppose you need to evaluate the arithmetic expression 11 / 4 / 6
.
11 / 4 / 6
[1] 0.4583333
In the above R code, R will evaluate the division operation from left to right. That is first evaluate 11 / 4
and then the result of 11 / 4
is divided by 6
.
Suppose we use parenthesis 11 / (4 / 6)
. Then R will evaluate the parenthesis first and then 11
is divided by the result of (4 / 6)
.
11 / (4 / 6)
[1] 16.5
Example 3: Operator Precedence in R
Suppose you want to evaluate the expression $\frac{10+5}{2}$. If you write the expression like 10 + 5 /2
, R will first evaluate the division 5 / 2
and then add the result to 10
.
10 + 5 / 2
[1] 12.5
To overcome this, use parenthesis at appropriate place, like (10 + 5)/2
.
(10 + 5)/2
[1] 7.5
In this case, R first evaluate the parenthesis (10 +5)
and then the result will be divided by 2
.
Example 4: Operator Precedence in R
Suppose we have the expression - 2 ^ 2: 2 ^3
. In the expression, there are three operators : unary minus (-
), exponent (^
) and sequence :
. Among these three operators, exponent (^
) has the highest precedence, then unary minus (-
) and then sequence operator (:
).
- 2 ^ 2:2 ^ 3
[1] -4 -3 -2 -1 0 1 2 3 4 5 6 7 8
The evaluation of expression is as follows:
- First R evaluate the exponent
2 ^ 2
(from left to right), resulting to4
. - Then evaluate
2 ^ 3
resulting to8
, - Then unary minus (
-
). That is attach the-
sign to2 ^ 2
, resulting to-4
. - And at the last, evaluate the sequence operator
-4 : 8
.
Example 5: Operator Precedence in R
Suppose we need to evaluate $4^{3^2}$. To evaluate this expression we write the R code as 4 ^ 3 ^ 2
.
4 ^ 3 ^ 2
[1] 262144
In the above R code, first 3 ^ 2
will be evaluated resulting to 9
. Then 4 ^
9
will be evaluated.
When we use the R code (4 ^ 3) ^ 2
the answer will be as follows:
(4 ^ 3)^2
[1] 4096
And when we use the R code 4 ^ (3 ^ 2)
the answer will be as follows:
4 ^ (3 ^ 2)
[1] 262144
Note that when there are more than one exponent operators (^
) in an expression, R will evaluate the exponent (^
) from right to left.
Endnote
In this tutorial you learned about precedence of operators in R.
To learn more about other operators in R, please refer to the following tutorials:
Assignment operators in R
Arithmetic operators in R
Relational operators in R
Logical operators in R
Miscellaneous of operators in R
Operators in R
Hopefully you enjoyed learning this tutorial on precedence of operators in R.