R Operators

In this tutorial you will learn about the R Operators and its application with practical example.

R Operators

An operator is a special symbol that is used to carry out some specific operation on its operand. In R Programming, we have rich set of built in operators to carry out different type of operations. There are operators for assignment, arithmetic operations, logical operations and comparison operations etc. In R, operators can be used with many types of variables or constants, but some of the operators are restricted to work on specific data types. Most operators are binary, meaning they take two operands, but a few are unary and only take one operand.

Type of operators in R Programming

R supports the following types of operators –

  • Arithmetic Operators
  • Assignment Operators
  • Relational Operators
  • Logical Operators

R Arithmetic Operators

Arithmetic Operators are used to perform arithmetic operations like addition, subtraction, multiplication, division, modulus, exponent, etc.

Let variable a holds 2 and variable b holds 5, then −

R Arithmetic operators
Operator Name Description Example
+ Addition Addition of given operands
a+b returns 7
- Subtraction Subtraction of second operand from first a-b returns -3
* Multiply Multiplication of given operands a*b returns 10
/ Division Returns Quotient after division
b/a returns 2.5
%/% Integer Division Returns Integer Quotient after division b%/%a returns 2
%% Modulus Returns Remainder after division b%%a returns 1
^ Exponent Raised to the exponent b^a returns 25

Example:-

R Assignment Operators

Assignment operators are used to assign value to a variable, you can assign a variable value or the result of an arithmetical expression.

Recommended:-  R Operator Precedence
R Assignment Operators
Operator Description
<-, <<-, = Leftwards assignment
->, ->> Rightwards assignment

here, <- and = are commonly used for assigning values, they can also be used interchangeably.

Example:-

R Relational Operators

Relational Operators are used evaluate a comparison between two operands. The result of a relational operation is a Boolean value that can only be true or false. Relational Operators are also referred as Comparison operators.

R Relational Operators
Operator Description
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to

Example:-

R Logical Operators

Logical operators are used to combine expressions with conditional statements using logical (AND,OR,NOT) operators, which results in true or false.

R Logical Operators
Operator Description
! Logical NOT
& Element-wise logical AND
&& Logical AND
| Element-wise logical OR
|| Logical OR
Recommended:-  R Lists

Example:-

In this tutorial we have learn about the R Operators and its application with practical example. I hope you will like this tutorial.