Operators in R

Operators in R

  • R
  • 7 mins read

This article will cover what operators are and how many types of operators are available in the R programming language. 

Operators in the R language are basically the symbols that direct the compiler to perform the specified task between the operand or operands enclosing them. Some of these operators require a single operand for their functioning, like the increment or the decrement operator, as well as the negation operator, while others function over a set of operands enclosing it, such as mathematical operators. 

There is a wide range of operators available within the R programming domain, which are as follows ; 

Arithmetic Operators in R

The arithmetic operators govern the mathematical operations, including addition, subtraction, multiplication, division, and power as well as module operations respectively. Now, we will discuss the arithmetic operators in detail. 

Addition Operator (+) in R

The addition operator is enclosed within two operands, and adds the results of both of these operands. The operands may be R objects, that is, either variables, matrices, or vectors. In the case of multi-dimensional R objects, the elements at the corresponding positions are added. 

#declaring two variable numbers 
num1 = 4
num2 = 5
#adding two numbers
res1 = num1+num2
cat("num1+num2 : ", res1)
The code produces the below output : 
num1+num2 :  9

Subtraction Operator (-) in R

The second operand value is subtracted from the first operand value in the case of the subtraction operator. The operation takes place at corresponding positions of both the R operand objects. 

#declaring two vectors of numbers 
num1 = c(4,3)
num2 = c(2,6)
#subtracting the vectors
res1 = num1 - num2

#printing the results
cat("num1 : " , num1)
cat("num2 : " , num2)
cat("num1-num2 : ", res1)
The output produced by the code is as follows : 
num1 : 4 3
num2 : 2 6
num1-num2 :  2 -3

Multiplication Operator (*) in R

The corresponding positions in both the R operand objects are multiplied by each other in this case. It may be applied over any type of R object, array, matrix, or list. 

​​#declaring two vectors of numbers 
num1 = c(4i,3)
num2 = c(2i,-6)
#subtracting the vectors
res1 = num1 * num2

#printing the results
cat("num1" , num1)
cat("num2" , num2)
cat("num1*num2 : ", res1)
The output produced by the code is : 
num1 0+4i  3+0i
num2 0+2i  -6+0i
num1*num2 :  -8+0i  -18+0i

Division Operator (/) in R

The first operand is divided by the second operand in the case division operation. Any numerical divided by the number 0 is equivalent to Infinity.

#declaring two vectors of numbers 
num1= matrix(c(12i,6),nrow=2,ncol=1) 
num2= matrix(c(4i,-3),nrow=2, ncol=1)
#subtracting the vectors
res1 = num1 / num2

#printing the results
cat("num1" , num1)
cat("num2" , num2)
cat("num1/num2 : ", res1)
Output
num1 0+12i 6+0i
num2 0+4i 3+0i
num1/num2 :  3+0i 2+0i

Power Operator (^) in R

The power operator is used to perform the operation or raise operand 1 by power operand 2. The power may be fractional or an integer in nature. 

#declaring two vectors of numbers 
num1= 4
num2= 1/2
#powerof numbers
res1 = num1 ^ num2

#printing the results
cat("num1 : " , num1)
cat("num2 : " , num2)
cat("num1^num2 : ", res1)
Output
num1 :  4
num2 :  3
num1%%num2 :  1

Comparison of Relational Operators in R

As the name suggests, the comparison operators are used to simulate comparisons between the specified operands. Each corresponding set of elements returns a boolean TRUE or FALSE value depending on whether the result of the operation is satisfied or not. 

There are basically six types of comparison operators, which are, as described in the table below, 

OperatorTerminologyResult
<Less than Returns true if op1 < op2
<=Less than equal to Returns true if op1 <= op2, else false
>Greater than Returns true if op1>op2
>= Greater than equal to Returns true if op1>=op2
==Equal toReturns true if op1=op2
!=Not equal toReturns true if op1 is not equal to op2

These operators can be applied over numerals, vectors, or strings. In the case of strings, the result is computed lexicographically. The string appearing first lexicographically is smaller. The below code snippet illustrates the usage of all these operators. 

#declaring two vectors of numbers 
vec1= c("a","d","F")
vec2= c("a","e","f")

print("<")
print(vec1<vec2)

print("<=")
print(vec1<vec2)

print("==")
print(vec1==vec2)

#declaring two vectors 
vec3 <- c(4,2,3)
vec4 <- c(5,1,3)

print(">")
print(vec3>vec4)

print(">=")
print(vec3>=vec4)

print("!=")
print(vec3!=vec4)
Output
[1] "<"
> print(vec1<vec2)
[1] FALSE  TRUE FALSE
> 
> print("<=")
[1] "<="
> print(vec1<vec2)
[1] FALSE  TRUE FALSE
> 
> print("==")
[1] "=="
> print(vec1==vec2)
[1]  TRUE FALSE FALSE
> 
> #declaring two vectors 
> vec3 <- c(4,2,3)
> vec4 <- c(5,1,3)
> 
> print(">")
[1] ">"
> print(vec3>vec4)
[1] FALSE  TRUE FALSE
> 
> print(">=")
[1] ">="
> print(vec3>=vec4)
[1] FALSE  TRUE  TRUE
> 
> print("!=")
[1] "!="
> print(vec3!=vec4)
[1]  TRUE  TRUE FALSE

Logical Operators in R

Logical operators are also known as decision operators and are used to make decisions based on the values stored in the corresponding positions. The value returned is either a boolean TRUE or FALSE. 

Element wise AND operation (&) in R

The elements at the corresponding positions of both the operands are compared and a boolean TRUE is returned if both the operand values are TRUE or non-zero, else FALSE is returned. 

#declaring vectors 
vec1 <- c(TRUE, 0)
vec2 <- c(7.4, 43)
print("& operator")
print(vec1 & vec2)
Output
[1] "& operator"
[1]  TRUE FALSE

Element wise OR operation (|) in R

The elements at the corresponding positions of both the operands are compared and a boolean TRUE is returned if any of the operand values are TRUE or non-zero, else FALSE is returned only if both are FALSE. 

#declaring vectors 
vec1 <- c(TRUE, 0)
vec2 <- c(7.4, 43)
print("| operator")
print(vec1 | vec2)
Output
[1] "| operator"
[1]  TRUE TRUE

Not (!)

This operator is a unary operator and it simply negates the state of a variable, that is, it reverses the TRUE state to FALSE and vice-versa

#declaring vectors 
vec1 <- c(TRUE, 0)

print("! operator")
print(!vec1)
Output
[1] "! operator"
> print(!vec1)
[1] FALSE  TRUE

Logical AND (&&)

The logical and operator compares only the first operators of a vector, and returns a single TRUE value, if both are TRUE, else it returns a FALSE value.

#declaring vectors 
vec1 <- c(TRUE, 0)
vec2 <- c(7.4, 43)
print("&& operator")
print(vec1 && vec2)
Output
[1] "|| operator"
> print(vec1 || vec2)
[1] TRUE

Logical OR (||)

The logical OR operator compares only the first operators of a vector and returns a single TRUE value, if either of those is TRUE, else it returns a FALSE value.

#declaring vectors 
vec1 <- c(TRUE, 0)
vec2 <- c(7.4, 43)
print("|| operator")
print(vec1 || vec2)
Output
[1] "|| operator"
> print(vec1 || vec2)
[1] TRUE

Assignment Operators in R

The assignment operators are basically used to assign values to variables. They are broad of three types, the equal to, the left arrow, and the right arrow respectively. All the operators are similar in functionality. 

OperatorUsage
var = value
<-  or <<- var <- value
-> or ->>value -> var

In the following code snippet, all the variables are assigned the same value, just by using different operators. 

#assigning value 1 to var1

var1 = 1
var1 <- 1 
1 -> var1

Miscellaneous Operators in R

The miscellaneous operators belong to the special category and are of the following types : 

 %in% 

This operator is used to check for the occurrence of a value within a specified vector or list of values. It returns a boolean TRUE value, in case the element is contained within the list, else false. 

#using the colon operator 

seq <- 3:15

#check if 2 present in the seq
print(2 %in% seq)
#check if 14 present in the seq
print(14 %in% seq)
Output
[1] FALSE
[1] TRUE

Range (:) Operator in R

It is used to generate a sequence from the starting value before the color to the ending value after the color. 

#using the colon operator 
print(3:15)
Output
[1]  3  4  5  6  7  8  9 10 11 12 13 14 15