Operators in R

In this tutorial, you will learn about operators and the different types of operators used in the R programming language.

What are R operators?

In R operators play a vital role as in any other programming language. Operators are special symbols like +,-, %, &,|, &&, <, <=, == that perform some kind of mathematical operations in programming languages. These operators need certain values with which only they can perform logical or mathematical operations. These values are further known as operands.

In programs, operators are capable of manipulating data & variables. An operand is an object which is manipulated by applying specific operators.

R Variables

What are the types of R operators?

The different types of operators in R programming are listed below.

  1. Arithmetic Operators
  2. Logical Operators
  3. Relational Operators
  4. Assignment Operators
  5. Miscellaneous Operators
R Variables

1. R Arithmetic Operators

Arithmetic operators perform basic arithmetic operations with all basic data types (will discuss in our next tutorial) available in R. The arithmetic operations include

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Remainder
  • Quotient
  • Exponent

Table to Summarize arithmetic operations

Arithmetic Operations Operator Use Description
Addition + (A+B) Adds two operands A & B
Subtraction - (A-B) Subtracts operand B from A
Multiplication * (A*B) Multiply two operands A & B
Division / (A/B) Divides two operands A & B
Modulus %% (A%%B) Get remainder after division of operand A by operand B
Quotient %/% (A%/%B) Quotient after dividing Operand A by Operand B
Exponent ^ (A^B) Operand A raised to the power of Operand B

Program to show arithmetic Operations in R with integers


Arithmetic operators +   -   *   /   %%   %/%   ^

A <- 20       #value 20 assigned using leftward operator "<-" to variable A
B = 10        #value 10 assigned using EQUAL TO  operator "=" to variable B
print(A+B)    #Addition
print(A-B)    #subtraction
print(A*B)    #Multiplication
print(A/B)    #division
print(A%%B)   #Remainder
print(A%/%B)  #Quotient
print(A^B)    #power

 

Output:


> print(A+B)    #Addition
[1] 30
> print(A-B)    #subtraction
[1] 10
> print(A*B)    #Multiplication
[1] 200
> print(A/B)    #division
[1] 2
> print(A%%B)   #Remainder
[1] 0
> print(A%/%B)  #Quotient
[1] 2
> print(A^B)    #power
[1] 1.024e+13

Program to show arithmetic Operations in R with vectors


# Arithmetic operators  +   -   *   /   %%   %/%   ^ with vectors

A <- c(10, 7, 7)
B <- c(0, 9, 4)

print(A+B)    #Addition
print(A-B)    #subtraction
print(A*B)    #Multiplication
print(A/B)    #division
print(A%%B)   #Remainder
print(A%/%B)  #Quotient
print(A^B)    #power

 

Output:


[1] 10 16 11
[1] 10 -2  3
[1]  0 63 28
[1]       Inf 0.7777778 1.7500000
[1] NaN   7   3
[1] Inf   0   1
[1]        1 40353607     2401

Note: • NaN means “Not a number” & Inf means “infinite” • They are used with complex numbers (real and imaginary part) and numeric values. • Both are reserved words in R

R Logical Operators

Logical operators perform logical operations like AND, OR, NOT. The result of logical operations using logical operators always falls under two categories either True or False. The logical expressions like a&b (a AND b), a&&b (a AND b), a|b (a OR b), etc are defined to have values 1 if true and 0 for false. The types of logical operators are

  • AND ( &)
  • OR     (|)
  • NOT (!)
  • Long AND (&&)
  • Long OR (||)
R Variables

The figure represents the different logical operators' operations and their corresponding output as TRUE (1) and FALSE (0).

  • & is similar to multiplication 0 *0 =0 (F), 0*1 =0(F) etc.
  • | is similar to add operation 0+0=0(F),0+1=1(T) etc..
  • ! just opposite if 0,then result will be 1.

Table to Summarize logical operations

Logical Operations Operator Use Description
Short  AND   & (A&B) Element wise logical AND operator
Short  OR | (A|B) Element wise logical OR operator
NOT   ! (A!B) Element wise logical NOT operator
Long AND     && (A&&B) logical AND operator
Long      OR || (A||B) logical OR operator

Program to show logical operators with R with integer


#Logical Operator
# & | ! && ||


a <- 0 # logical FALSE
b <- 2 # logical TRUE

print ( a & b ) # logical AND element wise
print ( a | b ) # logical OR element wise
print ( !a ) # logical NOT element wise
print ( a && b ) # logical AND consolidated for all elements
print ( a || b ) # logical OR consolidated for all elements

 

Output:


[1] FALSE
[1] TRUE
[1] TRUE
[1] FALSE
[1] TRUE

Difference between short and long AND, OR operator

Logical operator Description
 &     | Shorter form performs elementwise comparison
&&    || The longer form evaluates the result by examining only the first element from left to right.

program to show difference of short and long AND , OR operators


#Logical Operator
# & && ||   |

A <- c(3.5,TRUE,2+5i)   #vector A
B <- c(2.5,TRUE,3+2i)   #vector B
print(A&B)   
print(A&&B)
print(A|B)
print(A||B)

 

Output:


[1] TRUE TRUE TRUE
[1] TRUE
[1] TRUE TRUE TRUE
[1] TRUE

In the given program A&B evaluates each element hence referred as element wise comparison, compares with each element returns with output TRUE TRUE TRUE

R Variables

In the case of A&&B compares from left to right and examines only the first pair of elements. In the below pictures 3.5 &&2.5 are compared and its result is TRUE so validates automatically the next set as TRUE in long-form and returns out TRUE. The same procedure follows in the case of long OR (||). Instead of && the logical operator is || which produces a result based on the above truth table.

R Variables

R Relational Operators

In R relational operators compares the relationship between elements. The output of relational operators are either TRUE (1) or FALSE (0).The types of relational operators are

  • Less than   
  • Greater than
  • equal to 
  • Less than equal to
  • Greater than equal to
  • Not equal to

Table to summarize relational operators 

Relational Operations Operator Use Description
Less than   (A<B) CHECK relation whether operand A is less than Operand B
Greater than   (A>B) CHECK relation whether operand A is greater than Operand B
equal to   == (A==B) CHECK relation whether operand A is equal to Operand B
Less than equal to     <= (A<=B) CHECK relation whether operand A is less than or equal to Operand B
Greater than equal to   >= (A>=B) CHECK relation whether operand A is greater  than or equal to Operand B
Not equal to   != (A!=B) CHECK relation whether operand A is not  equal to Operand B

Program to show relational operators in R


#Relational Operators
# <  >  == <= >=  != 

A <- 20       #value 20 assigned using leftward operator "<-" to variable A
B = 10        #value 10 assigned using Equal to operator "=" to variable B
print(AB)    #Greater than
print(A==B)    #equal to 
print(A<=B)    #Less than equal to
print(A>=B)   #Greater than equal to 
print(A!=B)  #Not equal to

 

Output:


> print(A print(A>B)    #Greater than
[1] TRUE
> print(A==B)    #equal to 
[1] FALSE
> print(A<=B)    #Less than equal to
[1] FALSE
> print(A>=B)   #Greater than equal to 
[1] TRUE
> print(A!=B)  #Not equal to
[1] TRUE

R Assignment Operators

The word assignment denotes the nature of the operation itself. The assignment operators are considered to assign values. The types of assignment operators are

  • Leftward Assignment operator  <-  ,<<-
  • Rightward Assignment  operator ->,->>
  • Equal to       =

Table to summarize the Assignment Operators

Assignment Operations Operator Use Description
Leftward operator

<-

<<-

(A<-3) (A<<-3)

· Value 3 gets assigned to A.

· In both cases assigns right-hand side value to a left-hand-side variable.

· The difference is regarding the scope of variables. The assignment operator <<- behaves like a global scope.

Rightward operator

->

->>

(1->B)

(1->>B)

· Assigns left-hand side value to right hand side.

· The ->> assignment have a global scope.

Equal to = (A=8) Assigns right-hand side value to left side operand/variable.

program using assignment operator


# R Assignment Operators
a = 7.8
print ( a )

a <- FALSE
print ( a )

454 -> a
print ( a )

a <<- 2.9
print ( a )

6+3i ->>a
print ( a )

 

Output:


[1] 7.8
[1] FALSE
[1] 454
[1] 2.9
[1] 6+3i

R Miscellaneous Operators

In R there are certain operators other than you have learned till now to perform certain operations that manipulates the data. They are as shown in below table

Operator use Description
:   (colon) A:B Prints series of numbers from operand A till operand B
%in% A%in%B Determines whether element  A belongs to B
%*% A%*%B Multiplication of a vector with its transpose.

Program using R Miscellaneous Operators


# R Miscellaneous Operators

a = 2:9
print ( a )


a = c(2, 7, 71)
b = 0
print ( b %in% a )


a = c(2, 7, 71)
b = 71
print ( b %in% a )

M = matrix(c(1,2,3,4), 2, 2, TRUE)
print ( M %*% t(M) )

 

Output:


[1] 2 3 4 5 6 7 8 9
[1] FALSE
[1] TRUE
     [,1] [,2]
[1,]    5   11
[2,]   11   25

Some of the terms and syntax in programs may not be familiar now just understand the concept we will dive to each concepts in our coming tutorials.