Home »
R Language
R Language Operators
Operators in R Language: In this tutorial, we are going to learn about the various operators with syntaxes and examples in R programming language.
Submitted by Bhavya Sri Khandrika, on March 21, 2020
Operators in R Language
Generally speaking, an operator is a symbol that gives proper commands to the compiler regarding a specific action to be executed. The operators are used for carrying out the mathematical or logical calculations or also sometimes manipulations are also done using these operators. R language is normally considered as the rich language with several built-in operators to make a programmer more comfortable while coding the program. Moving further let us find what all are the inbuilt operators that are offered by the R language to its users.
Concerning other programming languages like C, C++, Java, etc, here also we find such similar operators that help the coders while coding their program. The commonly used operators are as follows:
- Arithmetic operators
- Relational Operators
- Logical Operators
- Assignment operators
- Miscellaneous operators
1) Arithmetic Operators
The operators like +, -, *, /, %%, %/%, ^ fall under the category of the arithmetic operators. The main usage of each operator is explained in details in the following text,
Plus Operator (+): This operator helps in adding the two vectors.
Example:
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v+t)
Output
[1] 10.0 8.5 10.0
Minus Operator (-): This particular operator subtracts the second vector from the first one.
Example:
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v-t)
Output
[1] -6.0 2.5 2.0
Multiply Operator (*): It multiplies both vectors.
Example:
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v*t)
Output
[1] 16.0 16.5 24.0
Divide Operator (/): It mainly divides the first vector considered with the second one taken.
Example:
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v/t)
Output
[1] 0.250000 1.833333 1.500000
Remainder Operator (%%): It gives the remainder of the first vector that is considered with the second one.
Example:
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%%t)
Output
[1] 2.0 2.5 2.0
Division Operator (%/%): The result of the division of the first vector to the second one(quotient).
Example:
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v%/%t)
Output
[1] 0 1 1
Raised to the Power (^): The first vector raised to the second one.
Example:
v <- c( 2,5.5,6)
t <- c(8, 3, 4)
print(v^t)
Output
[1] 256.000 166.375 1296.000
2) Relational Operators
This is one of the sub-branches under the operators available in the R language. The main use of these operators is to find the relation between the two vectors considered.
The result of these operators is usually a boolean value.
Coming to the list of available relational operators in the R language are as follows:
Greater Than |
> |
Less Than |
< |
Equal To |
== |
Greater Than or Equal To |
>= |
Less Than or Equal To |
<= |
Not Equal To |
!= |
The above operators have their own meanings while executing the program.
Greater Than Operator (>)
The above operator helps the user in the process of determining whether the first number is greater than the second one.
Example:
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v>t)
Output
[1] FALSE TRUE FALSE FALSE
Less Than Operator (<)
This operator tells the user whether the first operator is less than the second one.
Example:
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v < t)
Output
[1] TRUE FALSE TRUE FALSE
Equal To Operator (==)
Finds whether the both values are same to each other or not.
Example:
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v == t)
Output
[1] FALSE FALSE FALSE TRUE
Greater Than or Equal To Operator (>=)
Confirms whether each component of the foremost vector is superior than or equivalent to the corresponding part of the subsequent vector.
Example:
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v>=t)
Output
[1] FALSE TRUE FALSE TRUE
Less Than or Equal To Operator (<=)
Verifies if each component of the initial vector is less than or equal to the equivalent element of the next vector.
Example:
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v<=t)
Output
[1] TRUE FALSE TRUE TRUE
Not Equal To Operator (!=)
Verifies if each component of the initial vector is not equal to the equivalent element of the following vector.
Example:
v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
print(v!=t)
Output
[1] TRUE TRUE TRUE FALSE
3) Logical Operators
The following table shows all the available logical operators in the R language. The below operators are applicable over the vectors which are complex, logical or numeric-only. Here the R compiled assumes that all the numbers that are greater than the one are assumed to possess a logic value true.
Here while the logical operators are considered then every element available in the first vector is usually compared with the corresponding next element of the second vector. Thus, the result of the above comparison gives us a Boolean value.
The logical operators offered by the R language are as follows:
Element-wise Logical AND operator |
& |
Element-wise Logical OR operator |
| |
Logical NOT operator |
! |
Logical AND operator |
&& |
Logical OR operator |
|| |
Element-wise Logical AND operator (&)
It is named as the Element-wise Logical AND operator. It combines each component of the primary vector with the equivalent part of the next vector and gives an output TRUE if both the fundamental aspects are TRUE.
Example:
v <- c(3,1,TRUE,2+3i)
t <- c(4,1,FALSE,2+3i)
print(v&t)
Output
[1] TRUE TRUE FALSE TRUE
Element-wise Logical OR operator (|)
It is termed as Element-wise Logical OR operator. It combines each element of the primary vector with the matching element of the subsequent vector and gives an output TRUE if one of the elements is TRUE.
Example:
v <- c(3,0,TRUE,2+2i)
t <- c(4,0,FALSE,2+3i)
print(v|t)
Output
[1] TRUE FALSE TRUE TRUE
Logical NOT operator (!)
It is usually called with the name Logical NOT operator. The main function of this operator is that it takes each component of the vector and gives the contradictory logical value.
Example:
v <- c(3,0,TRUE,2+2i)
print(!v)
Output
[1] FALSE TRUE FALSE FALSE
Logical AND operator (&&)
Named as Logical AND operator. It takes the initial component of the first vector and compares it with the second one and finally gives the TRUE value if and only if both are TRUE.
Example:
v <- c(3,0,TRUE,2+2i)
t <- c(1,3,TRUE,2+3i)
print(v&&t)
Output
[1] TRUE
Logical OR operator (||)
It is termed as the Logical OR operator. The function of this operator is that it considers the primary components of both the vectors and eventually returns the value as TRUE if and only if one of them turns out to be TRUE.
Example:
v <- c(0,0,TRUE,2+2i)
t <- c(0,3,TRUE,2+3i)
print(v||t)
Output
[1] FALSE
4) Assignment Operators
The main intention of using these assignment operators in the program is to assign some value to the variables or vectors declared.
There are two ways while assigning the vectors is considered in the R language. They are:
- Right assignment
- Left assignment
Right assignment:
The operators used for right assignment are as follows:
Example:
v1 <- c(3,1,TRUE,2+3i)
v2 <<- c(3,1,TRUE,2+3i)
v3 = c(3,1,TRUE,2+3i)
print(v1)
print(v2)
print(v3)
Output
[1] 3+0i 1+0i 1+0i 2+3i
[1] 3+0i 1+0i 1+0i 2+3i
[1] 3+0i 1+0i 1+0i 2+3i
Left assignment:
Similarly, the operators used while implementing the left assignment technique is as follows:
Example:
c(3,1,TRUE,2+3i) -> v1
c(3,1,TRUE,2+3i) ->> v2
print(v1)
print(v2)
Output
[1] 3+0i 1+0i 1+0i 2+3i
[1] 3+0i 1+0i 1+0i 2+3i
5) Miscellaneous Operators
These miscellaneous operators have their own specific purpose of usage in the program. They do not deal with either the logical or mathematical calculations or computations.
Operator |
Description |
Example |
: |
Colon operator: This operator creates the series of numerical in sequence for a considered vector. |
v <- 2:8
print(v)
Output
[1] 2 3 4 5 6 7 8
|
%in% |
This operator is mainly used to recognize if an element/component belongs to the particular vector that is considered. |
v1 <- 8
v2 <- 12
t <- 1:10
print(v1 %in% t)
print(v2 %in% t)
Output
[1] TRUE
[1] FALSE
|
%*% |
This particular operator is used in the case when the multiplication of a matrix with its transpose is required to be performed. |
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
|