Table of contents
1.
Introduction
2.
What are the Operators in R?
3.
Types of Operators?
4.
Arithmetic Operators
4.1.
Addition Operator (+)
4.2.
Subtraction Operator (-)
4.3.
Multiplication Operator (*) 
4.4.
Division Operator (/) 
4.5.
Power Operator (^)
4.6.
Modulo Operator (%%)
5.
Relational Operators
5.1.
Less Than (<)
5.2.
Less Than Equal to (<=)
5.3.
Greater than (>)
5.4.
Greater than equal to (>=)
5.5.
Not equal to (!=) 
6.
Logical Operators
6.1.
Element-wise Logical AND operator (&)
6.2.
Element-wise Logical OR operator (|)
6.3.
NOT operator (!)
6.4.
Logical AND operator (&&)
6.5.
Logical OR operator (||)
7.
Assignment Operators
7.1.
Left Assignment (<- or <<- or =)
7.2.
Right Assignment (-> or ->>)
8.
Miscellaneous Operators
8.1.
%in% Operator
8.2.
%*% Operator
9.
Frequently Asked Questions
9.1.
What is parallel computing in R?
9.2.
What is time series analysis in R?
9.3.
What is exception handling in R?
10.
Conclusion
Last Updated: Feb 5, 2025
Medium

What are Various Operators Used in the R Language?

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The programming environment and language R was created especially for graphical depiction, data manipulation, and statistical analysis. R usually has a command-line interface. R is available on popular platforms such as Windows, Linux, and macOS. An operator is a symbol that represents an action in computer programming.

What are Various Operators Used in the R Language?

An operator is a symbol that guides the compiler to do specified logical or mathematical operations. R programming has a large number of built-in operators. In this article, we will discuss various types of operators in R along with their usage.

What are the Operators in R?

Operators are the symbols that instruct the compiler to perform various types of operations on the operands. Operators simulate different mathematical, logical, and decision processes on a set of Complex Numbers, Integers, and Numericals as input operands. Operators produce a result by acting on one or more operands (values or variables).

Types of Operators?

There are different kinds of operators in R programming, and each operator performs a particular task. There are various advanced operators for data manipulation, such as model formulas and list indexing.

Following are the types of operators in R:

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

Arithmetic Operators

Arithmetic operators in R simulate numerous math operations such as addition, subtraction, multiplication, division, and modulo. It is done by applying the provided operator across operands, which can be scalars, complex numbers, or vectors. Following are the various arithmetic operators:

Addition Operator (+)

The addition operator adds both operands' values at the respective locations.

Example:

var1 <- c( 2,5.5,6)
var2 <- c(8, 3, 4)
print(var1 + var2)

 

Output:

[1] 10.0  8.5  10.0

Subtraction Operator (-)

This operator subtracts the values of the second operand from the first and stores the result in the third operand.

Example:

var1 <- c( 2,5.5,6)
var2 <- c(8, 3, 4)
print(var1 - var2)

 

Output:

[1] -6.0  2.5  2.0

Multiplication Operator (*) 

The '*' operator is used to multiply corresponding elements of vectors and integers.

Example:

v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v*t)

 

Output:

[1] 16.0 16.5 24.0

Division Operator (/) 

The '/' operator is used to divide the first operand by the second operand.

Example:

v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v/t)

 

Output:

[1] 0.250000 1.833333 1.500000

Power Operator (^)

The first vector is raised to the exponent of the second vector.

Example:

a <- 4
b <- 5
print(a^b)

 

Output:

[1] 1024

Modulo Operator (%%)

When the first operand is divided by the second operand, this operator returns the remainder value.

Example:

a <- c(3,7.5,8)
b <- c(6, 4, 4)
print(a %% b)

 

Output:

[1] 3.0 3.5 0.0

Relational Operators

The relational operators in R perform comparison operations on the operands' corresponding elements. Returns a boolean TRUE value if the first operand fulfills the relation when compared to the second. A TRUE value is always judged to be greater than a FALSE value. Various relational operators are discussed below:

Less Than (<)

Returns TRUE if the first operand's matching element is less than the second operand's; otherwise, it returns FALSE.

Example:

var1 <- c(TRUE, 0.1,"apple")
var2 <- c(0,0.1,"bat")
print(var1 < var2)

 

Output:

[1] FALSE FALSE  TRUE

Less Than Equal to (<=)

This operator returns TRUE if the first operand's corresponding element is less than or equal to the second operand's. Otherwise, it returns FALSE.

Example:

v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v <= t)

 

Output:

[1]  TRUE FALSE TRUE TRUE

Greater than (>)

Returns TRUE if the first operand's matching element is greater than the second operand's. Otherwise, it returns FALSE.

Example:

var1 <- c(3,5.5,6,9)
var2 <- c(8,2.5,14,9)
print(var1 > var2)

 

Output:

[1] FALSE TRUE FALSE FALSE

Greater than equal to (>=)

This operator returns TRUE if the first operand's corresponding element is bigger or equal to the second operand's. Otherwise, it returns FALSE.

Example:

v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v >= t)

Output:

[1] FALSE TRUE FALSE TRUE

Not equal to (!=) 

If the matching element in the first operand is not equal to the second operand, then TRUE is returned otherwise returns FALSE.

Example:

var1 <- c(TRUE, 0.1,'apple')
var2 <- c(0,0.1,"bat")
print(list1!=list2)

 

Output:

[1] TRUE FALSE TRUE

Logical Operators

Based on the provided operator between the operands, logical operations in R simulate element-wise decision operations, which are subsequently evaluated to either a True or False boolean value. Any non-zero integer value, whether complex or real, is regarded as a TRUE value. Following are the various logical operators in R:

Element-wise Logical AND operator (&)

If both operands are True, it returns True; otherwise, it returns False.

Example:

var1 <- c(TRUE, 0.1)
var2 <- c(0,4+3i)
print(var1 & var2)

 

Output:

[1] FALSE TRUE

Element-wise Logical OR operator (|)

If one of the operands is True, this operator returns True; otherwise, it returns false.

Example:

var1 <- c(TRUE, 0.1)
var2 <- c(0,4+3i)
print(var1 | var2)

 

Output:

[1] TRUE  TRUE

NOT operator (!)

This unary operator negates the state of the operand's components.

Example:

list <- c(0,FALSE)
print( ! list)

 

Output:

[1] TRUE TRUE

Logical AND operator (&&)

If both of the operands' first elements are True, this operator returns True.

Example:

var1 <- c(TRUE)
var2 <- c(0)
print(var1 && var2)

 

Output:

[1] FALSE

Logical OR operator (||)

It returns True if one of the operands' first elements is True.

Example:

var1 <- c(TRUE)
var2 <- c(0)
print(var1 || var2)

 

Output:

[1] TRUE

Assignment Operators

In R, assignment operators are used to assign values to various data objects. Integers, vectors, and functions are examples of objects. These values are then saved using the variable names that have been allocated to them. Assignment operators are classified into two types: Right and left. These are discussed below:

Left Assignment (<- or <<- or =)

A vector is given a value using this operator.

Example:

vec1 = c("ab", TRUE)
print (vec1)

 

Output:

[1]  "ab"  "TRUE"

Right Assignment (-> or ->>)

It is also used to assign a value to a vector.

Example:

c("ab", TRUE) ->> vec1
print (vec1)

 

Output:

[1]  "ab"   "TRUE"

Miscellaneous Operators

These are the mixed operators in R that simulate the printing of sequences and vector assignment, either left or right-handed. The two miscellaneous operators in R are discussed below:

%in% Operator

It determines whether an element belongs to a list and returns TRUE if the value is present, else FALSE.

Example:

val1 <- 8
val2 <- 12
t <- 1:10
print(val1 %in% t) 
print(val2 %in% t) 

 

Output:

[1] TRUE
[1] FALSE

%*% Operator

This operator multiplies a matrix by its transpose. The matrix is transposed by swapping rows with columns and columns with rows. The first matrix's column count must be the same as the second matrix's row count. A square matrix is produced by multiplying matrix A by its transpose, B. 

Example:

M = matrix( c(2,6,5,1,10,4), nrow = 2,ncol = 3,byrow = TRUE)
t = M %*% t(M)
print(t)

 

Output:

    [,1]    [,2]
[1,]  65    82
[2,]  82    117

Frequently Asked Questions

What is parallel computing in R?

In R, parallel computing refers to the programming language's ability to distribute computational jobs over numerous processors or cores in a computer. This enables more efficient and faster processing of massive datasets and sophisticated algorithms.

What is time series analysis in R?

Time series analysis is a statistical tool for analyzing and modeling data patterns and trends over time. Time series analysis in R entails manipulating and analyzing time series data with functions and packages such as ts(), deconstruct(), and forecast().

What is exception handling in R?

The process of catching and treating problems or exceptions that may arise during the execution of an R script is referred to as exception handling in R. This is critical in preventing the script from crashing or terminating due to an unexpected error or exception.

Conclusion

In R, operators are essential for executing a wide range of mathematical and logical operations, allowing for rapid data manipulation and analysis. In this tutorial, we covered what an operator is and the different types of operators in R. We also discussed a working code example for each operator along with its output. 

To better understand the topic, you can refer to R Programming LanguageData Structures in R Programming, and R- environment

For more information, refer to our Guided Path on Coding Ninjas Studio to upskill yourself in PythonData Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more! 

Head over to our practice platform, Coding Ninjas Studio, to practice top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!

Live masterclass