CodeInfoNet

Learn programming on the go!

Home » Logical operators in R

Logical operators in R

In this article we are going to learn about the logical operators. I will cover the types of logical operators and how we can use them in our real life coding.

In previous article we have seen how to use variables in R programming. In case you have missed it, here is a link for the article using variables in R.

Let us know if the article is appropriate or if it needs any improvements. Mail us at admin@codeinfo.net or you can fill the feedback form at the end of this article.

Let’s kick off the topic!!


Logical operators in R

We have seen already that R programming have a data type as Logical. Now let’s see which logical operators R provides and how does they work.

In R programming language there are some logical operators, we are going to look at 10 of those.

  1. Equal to ==
  2. Not Equal to !=
  3. Less than <
  4. Greater than >
  5. Less than equal to <=
  6. Greater than equal to >=
  7. And &
  8. Or |
  9. Not !
  10. isTRUE()
  1. Equal to ==

This logical operator checks if the two values are equal. If they are equal it returns TRUE as a result

Ex.

equal <- 10 == 5

print(equal)
  1. Not Equal to !=

This logical operator also checks if the two values are equal. But it returns TRUE as a result if they are not equal

Ex.

notEqual <- 10 != 5

print(notEqual)
  1. Less than <

The less than ‘<’ operator gives result as true if the first value is less than the second value

Ex.

lessThan <- 5 < 10

print(lessThan)
  1. Greater than >

This operator will give True result when first value is greater than the second value

Ex.

greaterThan <- 5 > 10

print(greaterThan)

The advice here is to execute the given code snippets to get the better idea of these operators. Also, try using following two operators

  1. Less than equal to <=
  2. Greater than equal to >=
Logical operators
Logical operators
  1. And &

And operator gives the result as true only if both the values in an expression are TRUE.

Otherwise it returns false

Ex.

  x <- TRUE & TRUE

  x 
  1. OR |

Or operator returns true as a result if any of the value is true. It returns false only of both the values are FALSE

Ex.

  y <- TRUE | FALSE

  y
  1. Not !

Not operator is used to perform the negation of any  result.

If the not operator is applied to the result returning TRUE, it will return the result as FALSE and vice versa

Ex.

  not <- !(FALSE)

  print(not)
  1. IsTRUE()

This function is used to check if the result is true

Ex.

x <- 10 == 10

isTRUE(x)
Not operator
Not operator

Your feedback is important for us. Let us know if this article is helpful.

[wpforms id=”22″]

Sumit Rajguru

Back to top