R Tutorial R Charts & Graphs R Statistics
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

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.

+AdditionAdd two operands.Example
-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
%%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
!= 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.

Operator Description
and Returns True when all conditions are true
orReturns True when any of the conditions is true
notReturns opposite boolean result

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 below table describes other operators supported by R:

Operator Description
is Returns true when both variables are the same object a is b
is not Returns true when both variables are not the same object a is not b

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.

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
10+  -Addition, Subtraction
9<  <=  >  >=Less than, Less than or equal, Greater than, and Greater than or equal
==  !=Equality and Inequality
8!Logical NOT
7&  &&Logical AND
6|  ||Logical OR
5~as in formulae
4->  ->>Right assignment operator, Global right assignment operator
3<-  <<-Left assignment operator, Global left assignment operator (Right to Left)
2=Left assignment operator (Right to Left)
1?help (unary and binary)