An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. R language is rich in built-in operators and provides following types of operators.
Play millions of free games on your smartphone, tablet, computer, Xbox One, Oculus Rift, and more. Noun, plural R's or Rs, r's or rs. The 18th letter of the English alphabet, a consonant. Any spoken sound represented by the letter R or r, as in ran, carrot, or rhyme. Something having the shape of R. A written or printed representation of the letter R or r. A device, as a printer's type, for reproducing the letter R or r.
We have the following types of operators in R programming −
Following table shows the arithmetic operators supported by R language. The operators act on each element of the vector.
Operator | Description | Example |
---|---|---|
+ | Adds two vectors | Live Demo it produces the following result − |
− | Subtracts second vector from the first | Live Demo it produces the following result − |
* | Multiplies both vectors | Live Demo it produces the following result − |
/ | Divide the first vector with the second | Live Demo When we execute the above code, it produces the following result − |
%% | Give the remainder of the first vector with the second | Live Demo it produces the following result − |
%/% | The result of division of first vector with second (quotient) | Live Demo it produces the following result − |
^ | The first vector raised to the exponent of second vector | Live Demo it produces the following result − |
Following table shows the relational operators supported by R language. Each element of the first vector is compared with the corresponding element of the second vector. The result of comparison is a Boolean value.
Operator | Description | Example |
---|---|---|
> | Checks if each element of the first vector is greater than the corresponding element of the second vector. | Live Demo it produces the following result − |
< | Checks if each element of the first vector is less than the corresponding element of the second vector. | Live Demo it produces the following result − |
Checks if each element of the first vector is equal to the corresponding element of the second vector. | Live Demo it produces the following result − | |
<= | Checks if each element of the first vector is less than or equal to the corresponding element of the second vector. | Live Demo it produces the following result − |
>= | Checks if each element of the first vector is greater than or equal to the corresponding element of the second vector. | Live Demo it produces the following result − |
!= | Checks if each element of the first vector is unequal to the corresponding element of the second vector. | Live Demo it produces the following result − |
Following table shows the logical operators supported by R language. It is applicable only to vectors of type logical, numeric or complex. All numbers greater than 1 are considered as logical value TRUE.
Each element of the first vector is compared with the corresponding element of the second vector. The result of comparison is a Boolean value.
Operator | Description | Example |
---|---|---|
& | It is called Element-wise Logical AND operator. It combines each element of the first vector with the corresponding element of the second vector and gives a output TRUE if both the elements are TRUE. | Live Demo it produces the following result − |
| | It is called Element-wise Logical OR operator. It combines each element of the first vector with the corresponding element of the second vector and gives a output TRUE if one the elements is TRUE. | Live Demo it produces the following result − |
! | It is called Logical NOT operator. Takes each element of the vector and gives the opposite logical value. | Live Demo it produces the following result − |
The logical operator && and || considers only the first element of the vectors and give a vector of single element as output.
Operator | Description | Example |
---|---|---|
&& | Called Logical AND operator. Takes first element of both the vectors and gives the TRUE only if both are TRUE. | Live Demo it produces the following result − |
|| | Called Logical OR operator. Takes first element of both the vectors and gives the TRUE if one of them is TRUE. | Live Demo it produces the following result − |
These operators are used to assign values to vectors.
Operator | Description | Example |
---|---|---|
<− or = or <<− | Called Left Assignment | Live Demo it produces the following result − |
-> or ->> | Called Right Assignment | Live Demo it produces the following result − |
These operators are used to for specific purpose and not general mathematical or logical computation.
Operator | Description | Example |
---|---|---|
: | Colon operator. It creates the series of numbers in sequence for a vector. | Live Demo it produces the following result − |
%in% | This operator is used to identify if an element belongs to a vector. | Live Demo it produces the following result − |
%*% | This operator is used to multiply a matrix with its transpose. | Live Demo it produces the following result − |