R Tutorials
R Data Structures
R Graphics
R Statistics
R Examples
Operators are used to perform functions on variables and values.
In the example below, we use +
operator to add two values together:
10 + 5
R divides the operators in the following groups:
Arithmetic operators are used with numerical values to perform common mathematical operations:
Operator | Name | Example |
---|---|---|
+ | Addition | x + y |
- | Subtraction | x - y |
* | Multiplication | x * y |
/ | Division | x / y |
^ | Exponent | x ^ y |
%% | Modulus (Remainder from division) | x %% y |
%/% | Integer Division | x%/%y |
Shared operators used for variable pricing:
my_var <- 3
my_var <<- 3
3 -> my_var
3 ->>
my_var
my_var # print my_var
<< - is a global provider. You will learn more about this in the Global Variable chapter.
It is also possible to change the direction of the assignment conductor.
x <- 3 is equal to 3 -> x
Comparison operators are used to compare two values:
Operator | Name | Example |
---|---|---|
== | Equal | x == y |
!= | Not equal | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
Reasonable operators are used to compile conditional statements:
Operator | Description |
---|---|
& | Element-wise Logical AND operator. It returns TRUE if both elements are TRUE |
&& | Logical AND operator - Returns TRUE if both statements are TRUE |
| | Elementwise- Logical OR operator. It returns TRUE if one of the statement is TRUE |
|| | Logical OR operator. It returns TRUE if one of the statement is TRUE. |
! | Logical NOT - returns FALSE if statement is TRUE |
Various operators are used to manipulate data:
Operator | Description | Example |
---|---|---|
: | Creates a series of numbers in a sequence | x <- 1:10 |
%in% | Find out if an element belongs to a vector | x %in% y |
%*% | Matrix Multiplication | x <- Matrix1 %*% Matrix2 |