R Operators


Operator
Description
+
Add, 2 + 3 = 5
-
Subtract, 5 - 2 = 3
*
Multiply, 2 * 3 = 6
/
Divide, 6 / 2 = 3
^, **
Exponent, 2 ^ 3 = 8, 2 ** 3 = 8
%%
Modulus operator, 9%%2 = 1
%/%
Integer division, 9 %/% 2 = 4
<
Less than
>
Greater than
=
Equal to
<=
Less than or equal to
>=
Greater than or equal to
!=
Not equal to
!
Not
|
OR
&
And
:
define a sequence of integers
?:
a>b?c:d, if a>b, return c, otherwise d
%in%
whether x belongs to y

Define new operators:
Let's define "+" not as add, but as multiply:

>'+' <- function(x,y) x * y
>3 + 5

[1] 15


Let's delete the self defined operator "+":

>rm('+')
>3 + 5

[1] 8


Operator Precedence and Associativity:
Operator
Associativity
^
Right to Left
-x, +x
Left to Right (Unary minus, Unary plus)
:
Left to Right
%%
Left to Right
/, *
Left to Right
-, +
Left to Right
<, >, <=, >=, ==, !=
Left to Right
!
Left to Right
&, &&
Left to Right
|, ||
Left to Right
->, ->>
Left to Right
<-, <<-
Right to Left
=
Right to Left

> 3 * 4 - 2
[1] 10
> 1:4 + 1
[1] 2 3 4 5
> (1:4) + 1
[1] 2 3 4 5
> 1:(4 + 1)
[1] 1 2 3 4 5
>  1:4 * 2
[1] 2 4 6 8
>  1:4 ^ 2
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16
> -4:-2
[1] -4 -3 -2

endmemo.com © 2024  | Terms of Use | Privacy | Home