Logical operators carry out Boolean operations like AND, OR, NOT, etc. The logical operators can operate on raw, logical, and number-like vectors. Moreover, logical operators allow us to change or compare the results. There are various types of operators available in R, and logical is one of them; we will talk about the OR operator.

**OR in R**

The **OR** is a built-in** R logical operator** that returns **TRUE** if one of the conditions is **TRUE**. If both conditions are **FALSE,** then it will return **FALSE**.

This means that **TRUE** | **TRUE** equals **TRUE**, but **TRUE** | **FALSE** and **FALSE** | **TRUE** return to **TRUE**. Thus, when both logicals are **FALSE**, the result is **FALSE**, unlike the exclusive-OR operation in which it returns **TRUE**.

Remember that the OR operation is not an **exclusive or** operation, so **TRUE** | **TRUE** equals TRUE.

The difference between AND and OR operator is that the **AND** operator, only **TRUE** & **TRUE**, makes a **TRUE**; anything else is **FALSE**. Likewise, by using the OR operator, only **FALSE** | **FALSE** makes a **FALSE**; anything else is **TRUE**.

**Syntax**

**x | y**

**Return Value**

It returns **TRUE** if x or y is **TRUE**.

**Example**

Let’s define two logical vectors, which means they will contain logical values.

```
x <- c(TRUE, FALSE, 0, FALSE)
y <- c(FALSE, TRUE, 1, 0)
```

The **OR operator( | )** performs an element-wise operation on the vectors. Let’s use OR operator and see the output.

```
x <- c(TRUE, FALSE, 0, FALSE)
y <- c(FALSE, TRUE, 1, 0)
x | y
```

**Output**

`[1] TRUE TRUE TRUE FALSE`

The first three elements return **TRUE** because one of them is TRUE or 1. In the last element of both vectors, the values are FALSE, or 0, and both are FALSE. So, it returns **FALSE**.

**Example 2**

Let’s define a variable k, assign the value 19 and then check the value using the **OR **operator**.**

```
k <- 19
k < 21 | k > 10
```

**Output**

`[1] TRUE`

**Logical Operators in R**

Operator |
Description |

!x |
Not x |

x | y |
element-wise OR |

x | | y |
Logical OR |

x & y |
element-wise AND |

x && y |
Logical AND |

isTRUE(x) |
It tests if X is TRUE |

Operators & and | perform the element-wise operation, producing results having a length of the more extended operand. But && and || examine only the first element of the operands resulting in a single logical vector. **Therefore, **zero is considered **FALSE,** and non-zero numbers are taken as **TRUE**.

**AND Operator in R**

The **AND** **operator** in **R** is a built-in operator that takes two logical values and returns TRUE only if both values are TRUE themselves.

```
TRUE & TRUE
TRUE & FALSE
FALSE & TRUE
FALSE & FALSE
```

**Output**

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

You can see that it returns **TRUE** if both values are **TRUE; otherwise**, it returns **FALSE**.

You are not limited to using logical value with & operator. You can use the results of comparisons. For example, let’s say we have a variable equal to 11. To check if this variable is greater than 10 but less than 21, we can use “a” greater than 10 and a less than 21.

```
x <- 11
11 > 10 & 11 < 21
```

**Output**

`[1] TRUE`

**Final words**

The **OR** in **R** returns **TRUE** if one of the conditions is **TRUE**. If both conditions are **FALSE,** then it will return **FALSE**. That is it for the OR operator in R.

**Related posts**

Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.