Operators in R

In programming, an operator is a sign which represents an action. The operator in R is a symbol that tells the compiler to perform specific logical or mathematical manipulations. R programming is vast in built-in operators.

There are several types of operators in R, and every operator performs a different task. Here are some modern operators also such as model formula and list indexing for data manipulation.

In R programming following types of operators are used:

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators
  5. Miscellaneous Operators
operators in R
Types of operators in R

Arithmetic Operators in R

Arithmetic operators are the symbols that are used to represent arithmetic operations. The operators treat each and every element of the vector. In R programming, different arithmetic operators are supported.

S. NoOperatorDescription
1.+This operator is used to add two vectors in R programming. x <- y(2, 6.3, 5)
2.This operator is used for the division of a vector from another one. x <- y(2, 6.3, 5)
3.*This operator is used to multiply two vectors with each other. x <- y(2, 6.3, 5)
4./This operator divides the vector from another one. x <- y(2, 6.3, 5)
5.%%This operator is used to find the remainder of the first vector with the second vector. x <- y(2, 6.3, 5)
6.%/%This operator is used to find the division of the first vector with the second(quotient).
7.^This operator raised the first vector to the exponent of the second vector. x <- y(2, 6.3, 5)
Arithmetic Operators in R

Following is an example to perform arithmetic operations:

v1 <- c(2, 6)
v2 <- c(4, 8)
 
cat ("Addition :", v1 + v2, "\n")
cat ("Subtraction :", v1 - v2, "\n")
cat ("Multiplication :", v1 * v2, "\n")
cat ("Division :", v1 / v2, "\n")
cat ("Modulos :", v1 %% v2, "\n")
cat ("Power :", v1 ^ v2)

Output:

v1 2

Relational Operators in R

A relational operator is a symbol that works on various kinds of relations between two entities. It includes numerical balance and imbalance. A relational operator analyzes each component of the first vector with the answering component of the second vector. The comparison will be a Boolean value. Following relational operators are supported by R.

S. NoOperatorDescription
1.Will return TRUE when every element in the first vector is greater than the corresponding element of the second vector.
2.Will return TRUE when every element in the first vector is less than the corresponding element of the second vector.
3.<=Will return TRUE when every element in the first vector is less than or equal to the corresponding element of another vector.
4.>=Will return TRUE when every element in the first vector is greater than or equal to the corresponding element of another vector.
5.==Will return TRUE when every element in the first vector is equal to the corresponding element of the second vector.
6.!=Will return TRUE when every element in the first vector is not equal to the corresponding element of the second vector.
Relational Operators in R

Following is an example to perform relational operations:

v1 <- c(2, 6)
v2 <- c(4, 8)
 
cat ("Vector1 less than Vector2 :", v1 < v2, "\n")
cat ("Vector1 less than equal to Vector2 :", v1 <= v2, "\n")
cat ("Vector1 greater than Vector2 :", v1 > v2, "\n")
cat ("Vector1 greater than equal to Vector2 :", v1 >= v2, "\n")
cat ("Vector1 not equal to Vector2 :", v1 != v2, "\n")

Output:

o2 1

Logical Operators in R

The logical operators permit to make a decision on the basis of multiple conditions. Each operand is considered a condition that can be checked to the false or true value. The value of the condition is used to check the overall value of the op1 operator op2. Logical operators are applicable to those vectors whose type is complex, logical, or numeric.

The logical operator compares each element of the first vector with the corresponding element of the second vector.

Following are the basic types of operators which are supported by R:

S. NoOperator                    Description
1.&This is a logical AND operator. This operator takes the first element of both the vector and returns TRUE if both the elements are TRUE.
2.|This is a logical OR operator. This operator takes the first element of both the vector and returns TRUE if one of them is TRUE.
3.!This is a Logical NOT operator. This operator takes the first element of the vector and gives the opposite logical value as a result.
4.&&This operator takes the first element of both the vector and gives TRUE as a result, only if both are TRUE.
5.||This operator takes the first element of both the vector and gives the result TRUE if one of them is true.
Logical Operators in R

Following is an example to perform logical operations:

v1 <- c(2, 8)
v2 <- c(TRUE,FALSE)
 
cat ("Element wise AND :", v1 & v2, "\n")
cat ("Element wise OR :", v1 | v2, "\n")
cat ("AND :", v1 && v2, "\n")
cat ("OR :", v1 || v2, "\n")
cat ("Negation :", !v1)

Output:

o3 2

Assignment Operators in R

To assign a new value to a variable an assignment operator is used. In R, these operators are used to assign values to vectors. There are the following types of assignment operators in the R language.

S. NoOperatorDescription
1.<- or = or <<-These operators are known as left assignment operators.
2.-> or ->>These operators are known as right assignment operators.
Assignment Operators in R

Following is an example to perform assignment operations:

v1 <- c(4:12)
c(4:12) ->> v2
v3 <<- c(4:12)
v4 = c(4:12)
c(4:12) -> v5

cat ("vector 1 :", v1, "\n")
cat("vector 2 :", v2, "\n")
cat ("vector 3 :", v3, "\n")
cat("vector 4 :", v4, "\n")
cat("vector 5 :", v5)

Output:

o4 1

Miscellaneous Operators in R

Miscellaneous operators are used for a special and specific purpose. These operators are not used for general mathematical or logical computation. R supported the following miscellaneous operators.

S. NoOperatorDescription
1.:The colon operator is used to create the series of numbers in sequence for a vector.
2.%in%This is used when we want to identify if an element belongs to a vector.
3.%*%It is used to multiply a matrix with its transpose.
Miscellaneous Operators in R

Following is an example to perform Miscellaneous operations:

m <- matrix (1:6, nrow = 1, ncol = 6)
print("Matrix elements using : ")
print(m)
p= m %*% t(m)
print("Product of matrices")
print(p,)
cat ("does 1 exist in product matrix :", "1" %in% p)

Output:

o5 1