## 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:

- > x <- 5
- > y <- 21
- >
- > cat("x + y returns: ",x+y,"\n")
- x + y returns: 26
- >
- > cat("x - y returns: ",x-y,"\n")
- x - y returns: -16
- >
- > cat("x * y returns: ",x*y,"\n")
- x * y returns: 105
- >
- > cat("x / y returns: ",x/y,"\n")
- x / y returns: 0.2380952
- >
- > cat("y %/% x returns: ",y%/%x,"\n")
- y %/% x returns: 4
- >
- > cat("y %% x returns: ",y%%x,"\n")
- y %% x returns: 1
- >
- > cat("y ^ x returns: ",y^x)
- y ^ x returns: 4084101
- >

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:

- > x <- 5
- > y <- 21
- > cat("x < y is: ",x<y,"\n")
- x < y is: TRUE
- > cat("x > y is: ",x>y,"\n")
- x > y is: FALSE
- > cat("x <= 5 is: ",x<=5,"\n")
- x <= 5 is: TRUE
- > cat("y >= 20 is: ",y>=20,"\n")
- y >= 20 is: TRUE
- > cat("y == 16 is: ",y==16,"\n")
- y == 16 is: FALSE
- > cat("x != 5 is: ",x!=5)
- 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:

- > x <- c(TRUE, FALSE, 0, 7)
- > y <- c(FALSE, TRUE, FALSE, TRUE)
- >
- > cat("!x returns: ",!x,"\n")
- !x returns: FALSE TRUE TRUE FALSE
- >
- > cat("x&y returns: ",x&y,"\n")
- x&y returns: FALSE FALSE FALSE TRUE
- >
- > cat("x&&y returns: ",x&&y,"\n")
- x&&y returns: FALSE
- >
- > cat("x|y returns: ",x|y,"\n")
- x|y returns: TRUE TRUE FALSE TRUE
- >
- > cat("x||y returns: ",x||y,"\n")
- x||y returns: TRUE
- >

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, 2

^{nd}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,

- > x <- 4
- > x
- [1] 4
- >
- > x = 10
- > x
- [1] 10
- >
- > 7 -> x
- > x
- [1] 7
- >

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 + 5 * 3
- [1] 16
- >

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 + 5) * 3
- [1] 18
- >

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:

- > 3 / 4 / 5
- [1] 0.15
- >

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:

- > 3 / (4 / 5)
- [1] 3.75
- >

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.

Recommended Free Ebook

Similar Articles