R - Operators - GeeksforGeeks

10 Apr, 2022
Operators are the symbols directing the compiler to perform various kinds of operations between the operands. Operators simulate the various mathematical, logical, and decision operations performed on a set of Complex Numbers, Integers, and Numericals as input operands. 
R supports majorly four kinds of binary operators between a set of operands. In this article, we will see various types of operators in R Programming language and their usage.
Arithmetic Operators
Logical Operators
Relational Operators
Assignment Operators
Miscellaneous Operator
Arithmetic operations simulate various math operations, like addition, subtraction, multiplication, division, and modulo using the specified operator between operands, which may be either scalar values, complex numbers, or vectors. The operations are performed element-wise at the corresponding positions of the vectors. 
The values at the corresponding positions of both the operands are added. Consider the following R snippet to add two vectors:
Input : a <- c (1, 0.1)
        b <- c (2.33, 4)
        print (a+b)
Output : 3.33  4.10 
The second operand values are subtracted from the first. Consider the following R snippet to subtract two variables:
Input : a <- 6
        b <- 8.4
        print (a-b)
Output : -2.4 
The multiplication of corresponding elements of vectors and Integers are multiplied with the use of ‘*’ operator.
Input : B= matrix(c(4,6i),nrow=1,ncol=2) 
        C= matrix(c(2,2i ),nrow=1, ncol=2)
        print (B*C)
Output : 8+0i  -12+0i
The elements at corresponding positions of matrices are multiplied. 
The first operand is divided by the second operand with the use of ‘/’ operator.
Input : a <- 1
        b <- 0
        print (a/b)
Output : -Inf
The first operand is raised to the power of the second operand.
Input : list1 <- c(2, 3)
        list2 <- c(2,4)
        print(list1^list2)
Output : 4  81
The remainder of the first operand divided by the second operand is returned.
Input : list1<- c(2, 3)
        list2<-c(2,4)
        print(list1%%list2)
Output : 0  3
The following R code illustrates the usage of all Arithmetic Operators in R: 
R
# R program to illustrate# the use of Arithmetic operatorsvec1 <- c(0, 2)vec2 <- c(2, 3) # Performing operations on Operandscat ("Addition of vectors :", vec1 + vec2, "\n")cat ("Subtraction of vectors :", vec1 - vec2, "\n")cat ("Multiplication of vectors :", vec1 * vec2, "\n")cat ("Division of vectors :", vec1 / vec2, "\n")cat ("Modulo of vectors :", vec1 %% vec2, "\n")cat ("Power operator :", vec1 ^ vec2)
Output: 
Addition of vectors : 2 5 
Subtraction of vectors : -2 -1 
Multiplication of vectors : 0 6 
Division of vectors : 0 0.6666667 
Modulo of vectors : 0 2 
Power operator : 0 8
Logical operations simulate element-wise decision operations, based on the specified operator between the operands, which are then evaluated to either a True or False boolean value. Any non zero integer value is considered as a TRUE value, be it complex or real number. 
Returns True if both the operands are True.
Input : list1 <- c(TRUE, 0.1)
        list2 <- c(0,4+3i)
        print(list1 & list2)
Output : FALSE   TRUE
Any non zero integer value is considered as a TRUE value, be it complex or real number. 
Returns True if either of the operands are True.
Input : list1 <- c(TRUE, 0.1)
        list2 <- c(0,4+3i)
        print(list1|list2)
Output : TRUE  TRUE
A unary operator that negates the status of the elements of the operand.
Input : list1 <- c(0,FALSE)
        print(!list1)    
Output : TRUE  TRUE
Returns True if both the first elements of the operands are True.
Input : list1 <- c(TRUE, 0.1)
        list2 <- c(0,4+3i)
        print(list1 && list2)
Output : FALSE 
Compares just the first elements of both the lists.
Returns True if either of the first elements of the operands are True.
Input : list1 <- c(TRUE, 0.1)
        list2 <- c(0,4+3i)
        print(list1||list2)
Output : TRUE 
The following R code illustrates the usage of all Logical Operators in R:  
R
# R program to illustrate# the use of Logical operatorsvec1 <- c(0,2)vec2 <- c(TRUE,FALSE) # Performing operations on Operandscat ("Element wise AND :", vec1 & vec2, "\n")cat ("Element wise OR :", vec1 | vec2, "\n")cat ("Logical AND :", vec1 && vec2, "\n")cat ("Logical OR :", vec1 || vec2, "\n")cat ("Negation :", !vec1)
Output: 
Element wise AND : FALSE FALSE 
Element wise OR : TRUE TRUE 
Logical AND : FALSE 
Logical OR : TRUE 
Negation : TRUE FALSE
The relational operators carry out comparison operations between the corresponding elements of the operands. Returns a boolean TRUE value if the first operand satisfies the relation compared to the second. A TRUE value is always considered to be greater than the FALSE. 
Returns TRUE if the corresponding element of the first operand is less than that of the second operand. Else returns FALSE.
Input :  list1 <- c(TRUE, 0.1,"apple")
         list2 <- c(0,0.1,"bat")
         print(list1<list2)
Output :  FALSE FALSE  TRUE
Returns TRUE if the corresponding element of the first operand is less than or equal to that of the second operand. Else returns FALSE.
Input :  list1 <- c(TRUE, 0.1,"apple")
         list2 <- c(0,0.1,"bat")
         print(list<=list2)
Output : FALSE TRUE TRUE
Returns TRUE if the corresponding element of the first operand is greater than that of the second operand. Else returns FALSE.
Input :  list1 <- c(TRUE, 0.1,"apple")
         list2 list2)
Output : TRUE FALSE FALSE
Returns TRUE if the corresponding element of the first operand is greater or equal to than that of the second operand. Else returns FALSE.
Input :  list1 <- c(TRUE, 0.1,"apple")
         list2 =list2)
Output : TRUE TRUE FALSE
Returns TRUE if the corresponding element of the first operand is not equal to second operand. Else returns FALSE.
Input : list1 <- c(TRUE, 0.1,""apple")
        list2 <- c(0,0.1,"bat")
        print(list1!=list2)
Output : TRUE FALSE TRUE
The following R code illustrates the usage of all Relational Operators in R:
R
# R program to illustrate# the use of Relational operatorsvec1 <- c(0, 2)vec2 <- c(2, 3) # Performing operations on Operandscat ("Vector1 less than Vector2 :", vec1 < vec2, "\n")cat ("Vector1 less than equal to Vector2 :", vec1 <= vec2, "\n")cat ("Vector1 greater than Vector2 :", vec1 > vec2, "\n")cat ("Vector1 greater than equal to Vector2 :", vec1 >= vec2, "\n")cat ("Vector1 not equal to Vector2 :", vec1 != vec2, "\n")
Output: 
Vector1 less than Vector2 : TRUE TRUE 
Vector1 less than equal to Vector2 : TRUE TRUE 
Vector1 greater than Vector2 : FALSE FALSE 
Vector1 greater than equal to Vector2 : FALSE FALSE 
Vector1 not equal to Vector2 : TRUE TRUE 
Assignment operators are used to assign values to various data objects in R. The objects may be integers, vectors, or functions. These values are then stores by the assigned variable names. There are two kinds of assignment operators: Left and Right
Assigns a value to a vector.
Input : vec1 = c("ab", TRUE) 
        print (vec1)
Output : "ab"   "TRUE"
Assigns value to a vector.
Input : c("ab", TRUE) ->> vec1
        print (vec1)
Output : "ab"   "TRUE"
The following R code illustrates the usage of all Relational Operators in R:
R
# R program to illustrate# the use of Assignment operatorsvec1 <- c(2:5)c(2:5) ->> vec2vec3 <<- c(2:5)vec4 = c(2:5)c(2:5) -> vec5 # Performing operations on Operandscat ("vector 1 :", vec1, "\n")cat("vector 2 :", vec2, "\n")cat ("vector 3 :", vec3, "\n")cat("vector 4 :", vec4, "\n")cat("vector 5 :", vec5)
Output: 
vector 1 : 2 3 4 5 
vector 2 : 2 3 4 5 
vector 3 : 2 3 4 5 
vector 4 : 2 3 4 5 
vector 5 : 2 3 4 5
These are the mixed operators that simulate the printing of sequences and assignment of vectors, either left or right-handed. 
Checks if an element belongs to a list and returns a boolean value TRUE if the value is present else FALSE.
Input : val <- 0.1
        list1 <- c(TRUE, 0.1,"apple")
        print (val %in% list1)
Output : TRUE
Checks for the value 0.1 in the specified list. It exists, therefore, prints TRUE.
Prints a list of elements starting with the element before the color to the element after it.
Input :  print (1:5)
Output : 1 2 3 4 5
Prints a sequence of the numbers from 1 to 5.
This operator is used to multiply a matrix with its transpose. Transpose of the matrix is obtained by interchanging the rows to columns and columns to rows. The number of columns of first matrix must be equal to number of rows of second matrix. Multiplication of the matrix A with its transpose, B, produce a square matrix. 
Input :  mat = matrix(c(1,2,3,4,5,6),nrow=2,ncol=3)
         print (mat)
         print( t(mat))
         pro = mat %*% t(mat)
         print(pro)
Output :     [,1] [,2] [,3]      #original matrix of order 2x3
        [1,]   1    3    5
        [2,]   2    4    6
             [,1] [,2]           #transposed matrix of order 3x2
        [1,]    1    2
        [2,]    3    4
        [3,]    5    6
             [,1] [,2]          #product matrix of order 2x2
        [1,]   35   44
        [2,]   44   56
The following R code illustrates the usage of all Miscellaneous Operators in R:
R
# R program to illustrate# the use of Miscellaneous operatorsmat <- matrix (1:4, nrow = 1, ncol = 4)print("Matrix elements using : ")print(mat) product = mat %*% t(mat)print("Product of matrices")print(product,)cat ("does 1 exist in prod matrix :", "1" %in% product)
Output: 
[1] "Matrix elements using : "
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4

[1] "Product of matrices"
     [,1]
[1,]   30

does 1 exist in prod matrix : FALSE
arorakashish0911
kumar_satyam
smartsatyam840
Picked
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change column name of a given DataFrame in R
How to Replace specific values in column in R DataFrame ?
Adding elements in a vector in R programming - append() method
Loops in R (for, while, repeat)
Filter data by multiple conditions in R using Dplyr
Change Color of Bars in Barchart using ggplot2 in R
How to change Row Names of DataFrame in R ?
Convert Factor to Numeric and Numeric to Factor in R Programming
How to Change Axis Scales in R Plots?
Remove rows with NA in one column of R DataFrame