Operators In R

Introduction

 
In this article, we will learn about various operators provided by R language for performing different types of operations, including arithmetic, logical and bitwise operations.
 

R Operators

 
In R, an operator is a symbol that instructs the interpreter to perform a certain mathematical or logical operation. R comes with several built-in operators that you can use for such tasks. R operators are classified as follows:
  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators

Arithmetic Operators

 
These are the types of operators used to perform mathematical operations like addition, subtraction, division, etc. Here are the various arithmetic operators provided by R and the tasks we use them for:
  • + : Addition
  • - : Subtraction
  • *: Multiplication
  • / : Division
  • ^ : Exponent
  • %% : Modulus (Remainder from division)
  • %/% : Integer Division
Let us run the following set of scripts to demonstrate how the above operators can be used in R:
  1. > x <- 5  
  2. > y <- 21  
  3. >   
  4. > cat("x + y returns: ",x+y,"\n")  
  5. x + y returns:  26   
  6. >   
  7. > cat("x - y returns: ",x-y,"\n")  
  8. x - y returns:  -16   
  9. >   
  10. > cat("x * y returns: ",x*y,"\n")  
  11. x * y returns:  105   
  12. >   
  13. > cat("x / y returns: ",x/y,"\n")  
  14. x / y returns:  0.2380952   
  15. >   
  16. > cat("y %/% x returns: ",y%/%x,"\n")  
  17. y %/% x returns:  4   
  18. >   
  19. > cat("y %% x returns: ",y%%x,"\n")  
  20. y %% x returns:  1   
  21. >   
  22. > cat("y ^ x returns: ",y^x)  
  23. y ^ x returns:  4084101  
  24. >  
Just create an R script file and write the code there. Run it on the command prompt of your OS by invoking the Rscript interpreter. It will return the following:
 
 

Relational Operators

 
Relational operators help us make comparisons between values. Here is the list of operators supported in R:
  • > :Greater than
  • <= :Less than or equal to
  • >= :Greater than or equal to
  • == :Equal to
  • != :Not equal to
  • < :Less than
Consider the following example:
  1. > x <- 5  
  2. > y <- 21  
  3. > cat("x < y is: ",x<y,"\n")  
  4. x < y is:  TRUE   
  5. > cat("x > y is: ",x>y,"\n")  
  6. x > y is:  FALSE   
  7. > cat("x <= 5 is: ",x<=5,"\n")  
  8. x <= 5 is:  TRUE   
  9. > cat("y >= 20 is: ",y>=20,"\n")  
  10. y >= 20 is:  TRUE   
  11. > cat("y == 16 is: ",y==16,"\n")  
  12. y == 16 is:  FALSE   
  13. > cat("x != 5 is: ",x!=5)  
  14. x != 5 is:  FALSE>  
Write the script in an R script file and execute it from the command prompt of your OS. It will return the following output,
 
 

Logical Operators

 
We use these operators to perform Boolean operations such as AND, OR etc. Logical operators are only applicable to numeric, logical, or complex vectors. 
 
Here are the logical operators supported by R:
  • !: Logical NOT
  • & :Element-wise logical AND
  • && :Logical AND
  • | :Element-wise logical OR
  • || :Logical OR 
The & and | operators perform an element-wise operation to give a result having the length of the longer operand. The && and || will examine only the first element of the operands and returns a single length logical vector. Zero is considered to be FALSE while non-zero numbers are treated as TRUE.
 
The following example demonstrates how to use logical operators in R:
  1. > x <- c(TRUE, FALSE, 07)  
  2. > y <- c(FALSE, TRUE, FALSE, TRUE)  
  3. >   
  4. > cat("!x returns: ",!x,"\n")  
  5. !x returns:  FALSE TRUE TRUE FALSE   
  6. >   
  7. > cat("x&y returns: ",x&y,"\n")  
  8. x&y returns:  FALSE FALSE FALSE TRUE   
  9. >   
  10. > cat("x&&y returns: ",x&&y,"\n")  
  11. x&&y returns:  FALSE   
  12. >   
  13. > cat("x|y returns: ",x|y,"\n")  
  14. x|y returns:  TRUE TRUE FALSE TRUE   
  15. >   
  16. > cat("x||y returns: ",x||y,"\n")  
  17. x||y returns:  TRUE   
  18. >  
Write the script in an R script file and run it on the terminal of your OS. It will return the following result:
 
 
The ! x reversed the values stored in x. For True, it returned False and vice versa. For a 0, it returned a True and for a non-zero number, it returned a False.
 
In the other cases, each element is compared to other respective elements, that is, 1st element in x is compared to 1st element in y, 2nd element in x is compared to 2nd element in y, etc.
 

Assignment Operators

 
We use assignment operators to assign values to the variables. R supports the following assignment operators:
  • <-, <<-, = :Leftwards assignment
  • ->, ->> :Rightwards assignment
The = and <- operators can be used interchangeably to assign a variable a value in the same environment. The <<- operator should be used to assign values in parent environments similar to global assignments.
 
The rightward assignments are not very commonly used even though they are available. The following example demonstrates how to use these operators,
  1. > x <- 4  
  2. > x  
  3. [14  
  4. >   
  5. > x = 10  
  6. > x  
  7. [110  
  8. >   
  9. 7 -> x  
  10. > x  
  11. [17  
  12. >   
The above statements runs as follows:
 
 

Operator Precedence

 
When several operators are used in an expression, we need to know the order in which the execution will be done. Some operators are given high precedence over others, meaning that they will be executed first regardless of their position in the expression.
 
Consider the following example:
  1. 1 + 5 * 3  
  2. [116  
  3. >  
The expression will run as shown below:
 
 
The * operator was given a higher priority than the + operator, meaning that the expression was treated as 1 + (5 * 3). If you need to change the order of execution, you can enclose the parts in parenthesis (). For example:
  1. > (1 + 5) * 3  
  2. [118  
  3. >  
This will run as follows:
 
 
The expression placed within the parenthesis () was given the highest priority, hence, the result is not the same as what we got when not using parenthesis.
 

Operator Associativity

 
We are allowed to have more than one operator of similar precedence in the same expression. If that is the case, then the order of execution will be determined through associativity. Consider the example given below:
  1. 3 / 4 / 5  
  2. [10.15  
  3. >  
The expression will run as shown below:
 
 
The / has a left to right associativity. This means that the expression was evaluated as (3 / 4) / 5. If we need to change the order of execution, we can enclose the prioritized part within the parenthesis. For example:
  1. 3 / (4 / 5)  
  2. [13.75  
  3. >  
This will run as shown below:
 
 
The following table shows operator precedence and associativity in R. The operators have also been arranged according to decreasing order of precedence, that is, from highest to lowest:
 
 Operator symbol  Associativity
 ^  Right to Left
 -x, +x  Left to Right
 %%  Left to Right
 *, /  Left to Right
 
 Operator symbol  Associativity
 +, –  Left to Right
 <, >, <=, >=, ==,  Left to Right
 !  Left to Right
 &, &&   Left to Right
 
 

Summary

 
From the above article, we can conclude that Operators help us perform arithmetic and logical operations. R supports different types of operators. Arithmetic operators are used for performing mathematical operations. Relational operators help us perform various comparison operations.
 
Assignment operators help us assign values to variables.
 
Operator precedence determines the operator that is given the highest precedence. Operator associativity determines how operations are done when more than one operator with the same precedence are found in expression. 


Similar Articles