# R Operators: Arithmetic, Relational, Logical, Assignment

An operator in a programming language is a token that operates on a value or a variable. For example, The +(plus) is an operator to perform addition on the variables.

## R Operators

An operator in R is a symbol that tells the compiler to perform a particular mathematical or logical operations. R has inbuilt rich operators who have the following types.

### Types of operators

1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Miscellaneous Operators

### Arithmetic Operators in R

To add two vectors, use the + operator.

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

a + b``````

#### Output

`` 12 24 36``

It adds value to other vector’s respective values.

#### Subtract(-) operator in R

To subtract two vectors, use the – operator.

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

a - b``````

#### Output

`` -10 -20 -30``

#### Multiplication(*) operator in R

To multiply two vectors, use the * operator.

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

a * b``````

#### Output

`` 11 44 99``

#### Divide(/) operator in R

To divide two vectors, use the (/) operator.

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

a / b``````

#### Output

`` 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 4 27``

### Relational Operators in R

In 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

`` 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

`` FALSE FALSE FALSE``

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

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

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

a == b``````

#### Output

`` 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

`` 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

`` 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

`` 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 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

`` TRUE TRUE FALSE``

#### The | operator in R

The | is called 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

`` TRUE TRUE TRUE``

#### The ! operator in R

The ! is called 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

`` FALSE TRUE FALSE FALSE``

#### The && operator in R

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

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

a && b``````

#### Output

`` TRUE``

#### The || operator in R

The || operator is called the Logical OR operator. Takes the first item of both the 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

`` TRUE``

### Assignment Operators in R

Assign operators are used to assigning 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

`````` 11 21 1
 1 2 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

`````` 11 21 1
 1 2 1``````

### Miscellaneous Operators in R

Miscellaneous operators in R are used for specific purposes and 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 2 3 4``

#### %in% in R

The %in% operator is used to distinguish if an item belongs to a vector.

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

#### Output

`````` TRUE
 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.

Categories R