## Introduction to R Operators

R operators mean operators which are used in R. So, what does an operator mean? In simple terms, an operator asks the compiler to perform arithmetic, logical, bitwise manipulations using the operands. It is nothing but a symbol. R Operators article, let us first know what is R? R is a programming language that has started to been used widely by statisticians and data analysts as it is very versatile in statistical computing. It is open-source and free software. It first appeared in 1993.

### Operators

R Operators has many built-in operators. We will look at them one by one. Operators in R can be classified into four categories:

#### Arithmetic Operators

These R operators as the name suggest are used to carry out an operation like addition, subtraction, multiplication, division, exponent, modulus, Integer Division The symbols are shown below:

**(+) Addition Operator –**It adds two vectors.**(–) Subtraction Operator –**It subtracts the second vector from the first vector.**(*) Multiplication Operator –**It multiplies two vectors.**(/) Division Operator –**It divides the first vector with the second vector.**(% %) Modulus Operator –**It is used to give the remainder of the first vector with the second.**(%/%) Integer Division Operator**– It is used to give the quotient of the first vector with the second vector.**(^) Exponent Operator –**The first vector is raised to the exponent of the second vector.

Few examples are shown below:

x <- 6

> y <- 17

> x+y

[1] 23> x-y

[1] -9> x*y

[1] 102x <- 5

y <- 16

> y/x

[1] 3.2> y%/%x

[1] 3> y%%x

[1] 1These also work on vectors. Here are a few examples of arithmetic operators being used in vectors.

> x <- c(2,8,3)

> y <- c(6,4,1)

> x+y

[1] 8 12 4>x-y

[1] -4 4 2#### Relational Operators

As you can know by the name relation operator means the relationship between two values or compare between two values or two operands. Below is the list of symbols along with their operations :

**(<) Less than Operator –**It returns true for elements in the first vector which are less than the corresponding element present in the second vector.**(>)****Greater than Operator –**It returns true for elements in the first vector which are greater than the corresponding element present in the second vector.**(< =) Less than or Equal to**– As the name suggests, It returns true for elements in the first vector which is less than or equal to the corresponding element present in the second vector.**(> =) Greater than or Equal to –**It returns true for elements in the first vector which are greater than or equal to the corresponding element present in the second vector.**(= =) Equal to –**As the name suggests, It returns true for elements in the first vector which equal to the corresponding element present in the second vector.**(! =) Not Equal to –**It returns true for elements in the first vector which are not equal to the corresponding element present in the second vector.

Few examples are shown below:

4.5 (2,701 ratings)

View Course

> y <- 16

> x<y

[1] TRUE> x>y

[1] FALSE> x<=5

[1] TRUE> y>=20

[1] FALSE> y == 16

[1] TRUE> x != 5

[1] FALSEThese also work on vectors. Here are a few examples of arithmetic operators being used in vectors.

> x <- c(2,8,3)

> y <- c(6,4,1)

> x>y

[1] FALSE TRUE TRUE#### Logical Operators

Logical Operators are used for carrying out Boolean operations like AND, OR. Logical vectors are only applicable to vectors of logical type, numeric or complex. The numbers which are greater than one is true. Below is the list of logical operators and their operations.

**& –**It is an element-wise Logical AND Operator. It combines each element of the first vector with the corresponding element of the second vector and based on the result it returns TRUE or FALSE.**| –**It is an element-wise Logical OR Operator. returns TRUE or FALSE. It can return only one of the two available outputs.**! –**It is Logical NOT Operator. It works by giving the logical inverse of each element of the vector on which it is applied.**&& –**It is Logical AND Operator. Returns TRUE only if both the elements from the vectors is TRUE and it returns FALSE if any one or both of them are false.**|| –**It is a Logical OR Operator. Unlike the previous vector returns TRUE even if one of them is TRUE. It returns false if both are FALSE.

Few examples are shown below:

> x <- c(TRUE,FALSE,0,6)

> y <- c(FALSE,TRUE,FALSE,TRUE)

> !x

[1] FALSE TRUE TRUE FALSE> x&y

[1] FALSE FALSE FALSE TRUE> x&&y

[1] FALSE> x|y

[1] TRUE TRUE FALSE TRUE> x||y

[1] TRUE#### Assignment Operators

Assignment R operators are very simple as the name suggest, it is used to assign values to vectors. Below are the list of various operators and operations:

**<-,<<-, =:**It is called Leftwards assignment operators.**->, ->> :**It is called Rightward assignment operators.

The operators <- and = can be used to assign variables in the same environment and the operator <<- is generally used in global assignments.

Few examples are shown below:

> x <- 5

> x

[1] 5> x = 9

> x

[1] 9> 10 -> x

### Advantages Of R Operators

So, now let’s conclude this article with the various advantages that R has:

- It is free and an open source.
- It supports a wide variety of extensions for example data manipulations, statistical modeling, and graphics.
- They run in every operating system like Windows, Unix(such as Linux), Mac.
- It easily connects with other languages such as connecting and reading from a database using the Open Database Connectivity (ODBC) protocol.

### Recommended Articles

This has been a guide to R Operators. Here we discussed the Concept, Various Operators with Examples, and Advantages of R. You can also go through our other suggested Articles to learn more