Operators
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
Example
10 + 5
R divides the operators in the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Miscellaneous operators
R Arithmetic Operators
Arithmetic operators are used with numeric values to perform common mathematical operations:
| Operator | Name | Example | Try it |
|---|---|---|---|
| + | 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 |
R Assignment Operators
Assignment operators are used to assign values to variables:
Example
my_var <- 3
my_var <<- 3
3 -> my_var
3 ->>
my_var
my_var # print my_var
Note:
<<-is a global assigner. You will learn more about this in the Global Variable chapter. It is also possible to turn the direction of the assignment operator. x <- 3 is equal to 3 - > x
R Comparison Operators
Comparison operators are used to compare two values:
| Operator | Name | Example | Try it |
|---|---|---|---|
| == | 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 |
R Logical Operators
Logical operators are used to combine conditional statements:
| Operator | Description |
|---|---|
| & | Element-wise Logical AND operator. Returns TRUE if both elements are TRUE |
| && | Logical AND operator - Returns TRUE if both statements are TRUE |
| | | Elementwise- Logical OR operator. Returns TRUE if one of the statements is TRUE |
| | | |
| ! | Logical NOT - Returns FALSE if statement is TRUE |
R Miscellaneous Operators
Miscellaneous 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 |
Note: You will learn more about Matrix multiplication and matrices in a later chapter.