Training R operators in simple language – An operator is a symbol that tells a compiler to perform certain mathematical or logical operations
An operator is a symbol that tells a compiler to perform certain mathematical or logical operations.
The R language is very rich in terms of internal operators and provides the following types of operators:
Types of operators
In R programming, we have the following types of operators:
- Arithmetic operators
- Relational operators
- Logical operators
- Assignment operators
- Miscellaneous operators
Arithmetic operators
The table below shows the arithmetic operators supported by the R programming language. Operators act as a vector on each element.
operator | Operator explanation | Example |
+ | Combines two vectors. |
Execute the above code; Creates the following result: [1] 10.0 8.5 10.0 |
– | Subtracts the second vector from the first vector. |
Execute the above code; Creates the following result: [1] -6.0 2.5 ۲ 2.0 |
* | Multiplies two vectors |
Execute the above code; Creates the following result: [1] 16.0 16.5 24.0 |
/ | Divides the first vector by the second vector |
Execute the above code; Creates the following result: [1] 0. 250,000 1. 833333 1.500000 |
%% | Returns the remainder of dividing the first vector by the second vector |
Execute the above code; Creates the following result: [1] 2.0 ٫ 2.5 ۲ 2.0 |
% /% | Creates the result of dividing the first vector by the second (outside the part) |
Execute the above code; Creates the following result: [1] 0 1 1 |
^ | Bring the first vector to the power of the second vector. |
Execute the above code; Creates the following result: [1] 256,000 166,375 1296,000 |
Relational operators
The table below shows the relational operators supported by the R programming language. Each element of the first vector is compared with the corresponding element of the second vector. The result is a comparison of a boolean variable.
operator | Explain its function | Example |
> | Checks whether each element of the first vector; Is it larger than the corresponding elements in the second vector or not? |
Execution of the above code achieves the following result: [1] FALSE TRUE FALSE FALSE |
< | Checks whether each element of the first vector; Is it smaller than the corresponding element in the second vector or not? |
Execution of the above code achieves the following result: [1] TRUE FALSE TRUE FALSE |
== | Checks whether each element of the first vector is equal to the corresponding element in the second vector. |
Execution of the above code achieves the following result: [1] FALSE FALSE FALSE TRUE |
<= | Checks whether each element of the first vector is less than or equal to the corresponding element in the second vector. |
Execution of the above code achieves the following result: [1] TRUE FALSE TRUE TRUE |
<= | Checks whether each element is the first vector; Is greater than or equal to the corresponding element in the second vector. |
Execution of the above code achieves the following result: [1] TRUE FALSE TRUE TRUE |
! = | Checks whether each element of the first vector is equal to the corresponding element in the second vector. |
Execution of the above code achieves the following result: [1] TRUE TRUE TRUE FALSE |
Logical operators
The following is a table showing the logical operators that are supported in the R programming language. These operators can only be applied to vectors that are of the logical or complex type. All numbers greater than 1 are considered as the logical value of TRUE.
Each element of the first vector; Is compared with the corresponding element in the second vector. The result is shown as a boolean value.
Operator | Description | Example |
& | It is called element-to-element logical AND operator. This operator combines each element of the first vector with the corresponding element in the second vector, and if both variables are TRUE; Returns TRUE output. |
This code; Achieves the following result: [1] TRUE TRUE FALSE TRUE |
| | The logical operator OR is called element to element, and combines each element of the first vector with the corresponding element in the second vector, and if one of the variables is TRUE; Returns TRUE output |
Execution of this code will give the following result: [1] TRUE FALSE TRUE TRUE |
! | This operator is called the NOT logical operator; Takes each element of the vector and returns the opposite logical value. |
Executing the above code returns the following result: [1] FALSE TRUE FALSE FALSE |
The logical operator considers && and only the first element of the vectors and creates a vector at the output that has only one element.
Operator | Explain the function of the operator | Example |
&& | It is called the logical AND operator. Take the first element of both vectors and only if both elements are TRUE; Returns the TRUE statement. |
Executing the above code produces the following result: [1] TRUE |
|| | The OR operator is called logical and takes the first element of both vectors and returns TRUE if one of them is TRUE. |
Executing the above code produces the following result: [1] FALSE |
Assignment operators
These operators are used to assign values to variables.
Operators | Explain their function | Example |
<- Or = Or << – | It is called left assignment. |
|
-> Or – >> | Is called the right allocation. |
Miscellaneous operators
These operators are used for specific purposes and are generally not mathematical or logical.
Operator | Explain the function of the operator | Example |
: | Two-point operator; Which creates a series of consecutive numbers for a vector. |
Execute this code; It has the following result: [1] 2 3 4 5 6 7 8 |
% in% | This operator is used to determine whether an element belongs to a vector or not. |
Execute the above code; The following is the result: [1] TRUE [1] FALSE |
% *% | This operator is used to multiply a matrix in its output. |
Execution of the above code achieves the following result: [, 1] [, 2] [1,] 65 82 [2,] 82 117 |