# R - Operators

Operators are used to perform operation on two operands. Operators in R can be categorized as follows:

• Arithmetic operators
• Comparison operators
• Logical operators
• Assignment operators
• Miscellaneous operators

## R Arithmetic operators

Arithmetic operators are used to perform arithmetic operations on two operands. These operators work on vectors, matrices and scalars. These operators act on each element of the vector and matrices.

OperatorNameDescriptionExample
-SubtractionSubtract one operand from another operand.Example
*MultiplicationMultiply two operands.Example
/DivisionDivide one operand by another operand.Example
^Exponent / PowerReturns first operand raised to the power of second operand.Example
**Exponent / PowerReturns first operand raised to the power of second operand.Example
%%ModuloReturns remainder of division operation.Example
%/%Floor divisionReturns quotient of division operation.Example

## R Comparison operators

Comparison operators are used to compare values of two operands. It returns true when values matches and returns false when values does not match.

Operator DescriptionExample
==EqualExample
!= Not equalExample
>Greater thanExample
<Less thanExample
>=Greater than or equal toExample
<=Less than or equal toExample

## R Logical operators

Logical operators are used to combine two or more conditions. Along with combining conditions it can also be applied on vectors of type logical, numeric or complex. All numbers other than 0 are considered as logical value TRUE.

Operator DescriptionExample
&Element-wise Logical AND operator :
It combines each element of the first vector with the corresponding element of the second vector and returns true if both of the elements are true, else returns false.
Note: Also used to combine conditions and returns true when all conditions are true.
Example
|Element-wise Logical OR operator :
It combines each element of the first vector with the corresponding element of the second vector and returns true if any of the elements is true, else returns false.
Note: Also used to combine conditions and returns true when any of the conditions is true.
Example
!Logical NOT operator :
It takes each elements of a vector and returns opposite logical value.
Example
&&Logical AND operator :
It combines first elements of two vectors and returns true if both of the elements are true, else returns false.
Note: Also used to combine conditions and returns true when all conditions are true.
Example
||Logical OR operator :
It combines first elements of two vectors and returns true if any of the elements is true, else returns false.
Note: Also used to combine conditions and returns true when any of the conditions is true.
Example

## R Assignment operators

Operator DescriptionExample
=Left Assignment operator: Assigns values of right hand side expression to left hand side operand.Example
<-Left Assignment operator: Same as (=), except (<-) can be used anywhere, whereas the operator (=) is only allowed at the top level.
<<-Global left Assignment operator: Same functionality as (<-) but as global assignment operator.
->Right Assignment operator: Assigns values of left hand side expression to right hand side operand.Example
->>Global right Assignment operator: Same functionality as (->) but as global assignment operator.

## R Miscellaneous operators

The table below describes other operators supported by R:

OperatorNameDescription
:Colon operatorUsed to check the presence of an element in a given vector (or matrix) and returns boolean value.
%in%Matching operatorUsed to check the presence of an element in a given vector (or matrix) and returns boolean value.
%*%Matrix Multiplication operatorMultiplies two matrices.

## R Operators Precedence

Operator precedence (order of operations) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given expression.

For example, multiplication has higher precedence than addition. Thus, the expression 1 + 2 × 3 is interpreted to have the value 1 + (2 × 3) = 7, and not (1 + 2) × 3 = 9. When exponent is used in the expression, it has precedence over both addition and multiplication. Thus 3 + 52 = 28 and 3 × 52 = 75.

The following table lists the precedence and associativity of R operators. Operators are listed top to bottom, in descending precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence.

Within an expression operators of equal precedence are evaluated from left to right except where indicated (Note that = is not necessarily an operator).

The binary operators ::, :::, \$ and @ require names or string constants on the right hand side, and the first two also require them on the left.

PrecedenceOperatorDescription
18::  :::access variables in a namespace
17\$  @component / slot extraction
16[ ]  [[ ]]indexing
15^Exponentiation operator (Right to Left)
14+a  -aUnary plus, Unary minus
13:Sequence operator
12%%  %*%  %/%  %in%  %o%  %x%Special operators
11*  /Multiplication, Division