## Introduction to R Operators

R Operators are the features supported by the R language for processing certain operations on the operands or participating variables and data values. The Operators in the R language are of the following types such as arithmetic, logical, relational, and assignment operators. The operators in the R language are represented through symbols and processed by the R runtime environment. For example, some of the commonly used arrive maniac operators are addition operators represented by ‘+’ symbol for adding two vector values in R, ‘=’ or ‘<- ‘ is the assignment operator used to assign values the variable. The R operators help to develop standardized program logic.

### Operators

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

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

> 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#### 3. 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 anyone 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#### 4. Assignment Operators

Assignment R operators are very simple as the name suggest, it is used to assign values to vectors. Below is 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 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.

- Overview of C Operators
- MySQL Functions and Operators
- Learn the easy way To Install Swift
- PHP Commands | Basic and Advanced
- Guide to Types of Assignment Operators in Java

13 Online Courses | 20 Hands-on Projects | 120+ Hours | Verifiable Certificate of Completion

4.5

View Course

Related Courses