# Logical Operators in R

In this tutorial we will learn about logical operators in R.

**Logical Operators in R – For Comparison**

Similar to other programming languages R also uses certain Logical operators to compare.

For example,

- ‘==’ – compares if two entities are equal or not and returns TRUE if values are equal
- ‘!=’ – returns TRUE if two entities are not equal
- ‘<‘ – returns TRUE if value on the left is lesser than the value on the right
- ‘<=’ – returns TRUE if value on the left is lesser than or equal to value on the right
- ‘>’ – returns TRUE if value on the left is greater than the value on the right
- ‘>=’ – return TRUE if value on the left is greater than or equal to the value on the right

Let see a few examples,

```
> 5==5
```

```
## [1] TRUE
```

```
> 5!=5
```

```
## [1] FALSE
```

```
> 5<5
```

```
## [1] FALSE
```

```
> 5<=5
```

```
## [1] TRUE
```

```
> 5>5
```

```
## [1] FALSE
```

```
> 5>=5
```

```
## [1] TRUE
```

```
> 4==5
```

```
## [1] FALSE
```

```
> 4!=5
```

```
## [1] TRUE
```

```
> 4<5
```

```
## [1] TRUE
```

```
> 4<=5
```

```
## [1] TRUE
```

```
> 4>5
```

```
## [1] FALSE
```

```
> 4>=5
```

```
## [1] FALSE
```

```
> 6==5
```

```
## [1] FALSE
```

```
> 6!=5
```

```
## [1] TRUE
```

```
> 6<5
```

```
## [1] FALSE
```

```
> 6<=5
```

```
## [1] FALSE
```

```
> 6>5
```

```
## [1] TRUE
```

```
> 6>=5
```

```
## [1] TRUE
```

Logical operators can applied on other data types like character, complex as well. e.g.,

```
> 'a'>'b'
```

```
## [1] FALSE
```

```
> 'R is a good language'>'R is a bad language'
```

```
## [1] TRUE
```

In case of Strings it compares the character by character based on their corresponding ASCII values.

A trick question for you – What will be the output of the following line of code?

```
> sum(TRUE,FALSE,TRUE,TRUE)
```

You must be having question – How will they work on vectors?

Let’s figure it out.

```
> v1<-c(1:10)
> v1
```

```
## [1] 1 2 3 4 5 6 7 8 9 10
```

```
> v1<5
```

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

Here, R is comparing each element of V1 with number 5.

Let’s create one more vector

```
> v2<-c(1:5,11:15)
> v2
```

```
## [1] 1 2 3 4 5 11 12 13 14 15
```

Now let see what will R return if we compare this two vectors.

```
> v1==v2
```

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

```
> v1<=v2
```

```
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
```

So, in case of a vector R will compare element by element and return the results accordingly.

**Other Logical operators in R**

Apart from comparison operators listed above we have few boolean operators as well.

In comparison operators we compare two entities and try to find out the relation between them.

There are few operators which work on Logical variables and see the relation.

- ‘&’ – AND Operator – compares whether both left and right entities are TRUE or not
- ‘|’ – OR Operator – compares whether any of the left and right entities are TRUE or not

Let see a few examples

```
#And Operator
```

```
> TRUE & TRUE
```

```
## [1] TRUE
```

```
> TRUE & FALSE
```

```
## [1] FALSE
```

```
> FALSE & FALSE
```

```
## [1] FALSE
```

```
> FALSE & TRUE
```

```
## [1] FALSE
```

```
#OR Operator
```

```
> TRUE | TRUE
```

```
## [1] TRUE
```

```
> TRUE | FALSE
```

```
## [1] TRUE
```

```
> FALSE & FALSE
```

```
## [1] FALSE
```

```
> FALSE & TRUE
```

```
## [1] FALSE
```

Question for you – What will be the output of following? (Hint: ! is a flipper)

```
> !TRUE
> !T & TRUE
> !FALSE & F
```

Let’s run and see

```
> !TRUE
```

```
## [1] FALSE
```

```
> !T & TRUE
```

```
## [1] FALSE
```

```
> !FALSE & F
```

```
## [1] FALSE
```

‘!’ is a negation operator which converts TRUE to FALSE and FALSE to TRUE

let’s create two logical vectors in an intelligent way

```
> v3<-v1<5
> v3
```

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

```
> v4<-v1<=7
> v4
```

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

Now let’s Run our logical operators on these two logical vectors.

```
> v3 & v4
```

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

So, R checks element by element whether both of the values are TRUE or not. So, basically first element of v3 is compared with first element of v4 and so on.

```
> v3 | v4
```

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

This ‘&’ and ‘|’ operators are known as bitwise operators which compares the element by element. There are two other boolean operators ‘&&’ and ‘||’.

Let’s see what is the difference between ‘|’, ‘&’ and ‘||’, ‘&&’ respectively.

‘&&’ and ‘||’ are known as short-circuit operators as well.

Before understanding the difference, let’s first create few logical vectors.

```
> v5<-c(T,T,T,F,T)
> v5
```

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

```
> v6<-c(F,F,T,T,F)
> v6
```

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

```
> v7<-c(T,T,T,T,T)
> v7
```

```
## [1] TRUE TRUE TRUE TRUE TRUE
```

Now, run short-circuit operators on these newly created vectors.

```
> v5 && v6
```

```
## [1] FALSE
```

```
> v5 && v7
```

```
## [1] TRUE
```

```
> v6 && v7
```

```
## [1] FALSE
```

These short-circuit operators only check the first elements and compares them.

#### analyticsfreak

#### Latest posts by analyticsfreak (see all)

- Few interesting questions related to correlation - July 22, 2016
- How to make a reproducible example to share? - July 21, 2016
- Few random questions on Random Forest - July 20, 2016