An **operator** in **R** is a symbol that tells the compiler to perform a mathematical or logical operation.

**Types of operators**

- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Miscellaneous Operators

**Arithmetic Operators in R**

**Add(+) operator in R**

To add two vectors, you can use the **“+ operator”**.

```
a <- c(1, 2, 3)
b <- c(11, 22, 33)
a + b
```

**Output**

`[1] 12 24 36`

It adds value to other vectors’ respective values.

**Subtract(-) operator in R**

To subtract two vectors, you can use the **“- operator”**.

```
a <- c(1, 2, 3)
b <- c(11, 22, 33)
a - b
```

**Output**

`[1] -10 -20 -30`

**Multiplication(*) operator in R**

To multiply two vectors, you can use the **“* operator”**.

```
a <- c(1, 2, 3)
b <- c(11, 22, 33)
a * b
```

**Output**

`[1] 11 44 99`

**Divide(/) operator in R**

You can use the **“(/) operator” to divide two vectors**.

```
a <- c(1, 2, 3)
b <- c(11, 22, 33)
a / b
```

**Output**

`[1] 0.09090909 0.09090909 0.09090909`

**Exponent(^) operator in R**

The first vector is raised to the exponent of the second vector.

```
a <- c(1, 2, 3)
b <- c(1, 2, 3)
a ^ b
```

**Output**

`[1] 1 4 27`

**Relational Operators in R**

In a relational operator, each item of the first vector is compared with the corresponding item of the second vector.

**Greater than(>) Operator in R**

It checks if each item of the first vector is greater than the corresponding element of the second vector.

```
a <- c(11, 21, 31)
b <- c(1, 2, 3)
a > b
```

**Output**

`[1] TRUE TRUE TRUE`

**Less than(<) Operator in R**

It checks if each item of the first vector is less than the corresponding element of the second vector.

```
a <- c(11, 21, 31)
b <- c(1, 2, 3)
a < b
```

**Output**

`[1] FALSE FALSE FALSE`

**Equal to(==) Operator in R**

It checks if each item of the first vector equals the corresponding element of the second vector.

```
a <- c(11, 21, 31)
b <- c(1, 2, 3)
a == b
```

**Output**

`[1] FALSE FALSE FALSE`

**Greater than equal to(>=) Operator in R**

It checks if each item of the first vector is greater than or equal to the corresponding item of the second vector.

```
a <- c(11, 21, 31)
b <- c(1, 2, 3)
a >= b
```

**Output**

`[1] TRUE TRUE TRUE`

**Less than equal to(<=) Operator in R**

It checks if each item of the first vector is less than or equal to the corresponding item of the second vector.

```
a <- c(11, 21, 31)
b <- c(1, 2, 3)
a <= b
```

**Output**

`[1] FALSE FALSE FALSE`

**Not equal to(!=) operator in R**

It checks if each item of the first vector is unequal to the corresponding item of the second vector.

```
a <- c(11, 21, 31)
b <- c(1, 2, 3)
a != b
```

**Output**

`[1] TRUE TRUE TRUE`

**Logical Operators in R**

The logical operators in R are applied only to vectors of type logical, numeric, or complex. Each item of the first vector is compared with the corresponding item of the second vector. The result of the comparison is a Boolean value.

**The & operator in R**

The & is called the element-wise Logical AND operator. It combines each item of the first vector with the corresponding element of the second vector and gives an output TRUE if both items are TRUE.

```
a <- c(11, 21, TRUE)
b <- c(1, 2, FALSE)
a & b
```

**Output**

`[1] TRUE TRUE FALSE`

**The | operator in R**

The | is called an element-wise Logical OR operator. It combines each item of the first vector with the corresponding item of the second vector and gives an output TRUE if one of the items is TRUE.

```
a <- c(11, 21, TRUE)
b <- c(1, 2, FALSE)
a | b
```

**Output**

`[1] TRUE TRUE TRUE`

**The ! operator in R**

The ! is called an element-wise NOT operator. It combines each item of the first vector with the corresponding item of the second vector and gives the opposite logical value.

```
a <- c(11, 0, TRUE, 2+1i)
!a
```

**Output**

`[1] FALSE TRUE FALSE FALSE`

**The && operator in R**

The && operator is called the Logical AND operator. It takes the first item of both vectors and gives the TRUE only if both are TRUE.

```
a <- c(11, 21, TRUE)
b <- c(1, 2, TRUE)
a && b
```

**Output**

`[1] TRUE`

**The || operator in R**

The || operator is called the Logical OR operator. It takes the first item of both vectors and gives the TRUE only if one of them is TRUE.

```
a <- c(11, 21, TRUE)
b <- c(1, 2, TRUE)
a || b
```

**Output**

`[1] TRUE`

**Assignment Operators in R**

Assign operators are used to assign values to vectors.

**Left assignment operators in R**

Three operators <-, <<-, and = operators are called left assignment operators.

```
a <- c(11, 21, TRUE)
b <<- c(1, 2, TRUE)
c = c(1+2i, TRUE, "R")
a
b
c
```

**Output**

```
[1] 11 21 1
[1] 1 2 1
[1] "1+2i" "TRUE" "R"
```

**Right assignment operators in R**

Three operators -> and ->> are called left assignment operators.

```
c(11, 21, TRUE) -> a
c(1, 2, TRUE) ->> b
a
b
```

**Output**

```
[1] 11 21 1
[1] 1 2 1
```

**Miscellaneous Operators in R**

R miscellaneous operators are used for specific purposes, not general mathematical or logical computation.

**Colon operator(:) in R**

The colon(:) operator creates a series of numbers in sequence for a vector.

```
rv <- 1:4
rv
```

**Output**

`[1] 1 2 3 4`

**%in% in R**

The **%in%** operator distinguishes if an item belongs to a vector.

```
rv <- 2
v <- 6
cj <- 1:5
print(rv %in% cj)
print(v %in% cj)
```

**Output**

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

**%*% in R**

The **%*%** operator in R is used to identify if an element belongs to a vector. Let’s see an example of the matrix.

```
mtrx <- matrix(c(21, 6, 51, 1, 10, 41), nrow = 2, ncol = 3, byrow = TRUE)
dt <- mtrx %*% t(mtrx)
print(dt)
```

**Output**

```
[,1] [,2]
[1,] 3078 2172
[2,] 2172 1782
```

That is it for the Operators in R.

Krunal Lathiya is a Software Engineer with over eight years of experience. He has developed a strong foundation in computer science principles and a passion for problem-solving. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language.