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.
- Equal to ==
- Not Equal to !=
- Less than <
- Greater than >
- Less than equal to <=
- Greater than equal to >=
- And &
- Or |
- Not !
- isTRUE()
- 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)
- 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)
- 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)
- 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
- Less than equal to <=
- Greater than equal to >=
- 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
- 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
- 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)
- IsTRUE()
This function is used to check if the result is true
Ex. x <- 10 == 10 isTRUE(x)
Your feedback is important for us. Let us know if this article is helpful.
[wpforms id=”22″]