Logical operator is one of a kind operators that used to compare two logical condition (TRUE/FALSE) or more.
Logical operators in R language are not too far different from logical operators in other programming languages.
Below are the logical operators in R language:
| Symbol | Description |
|---|---|
| “&” | If there’s a FALSE, then the value is FALSE |
| ” | ” |
| “&&” | If there’s a FALSE, then the value is FALSE |
| ” | |
| “!” | Reverse value (negation) |
Here’s the examples:
a <- c(TRUE, TRUE, FALSE)
x <- c(FALSE, TRUE, FALSE)
print(a & x)
## [1] FALSE TRUE FALSE
a <- c(TRUE, TRUE, FALSE)
x <- c(FALSE, TRUE, TRUE)
print(a | x)
## [1] TRUE TRUE TRUE
a <- c(TRUE, TRUE, FALSE)
x <- c(TRUE, TRUE, FALSE)
print(x && a)
## Warning in x && a: 'length(x) = 3 > 1' in coercion to 'logical(1)'
## Warning in x && a: 'length(x) = 3 > 1' in coercion to 'logical(1)'
## [1] TRUE
a <- c(TRUE, TRUE, FALSE)
x <- c(FALSE, TRUE, FALSE)
print(x || a)
## Warning in x || a: 'length(x) = 3 > 1' in coercion to 'logical(1)'
## Warning in x || a: 'length(x) = 3 > 1' in coercion to 'logical(1)'
## [1] TRUE
x <- c(FALSE, TRUE, FALSE)
print(!x)
## [1] TRUE FALSE TRUE
AND (&) and OR (|) operator compare one by one of the boolean element. Checking from left to right.
To see the differences between AND/OR with AND/OR wise elements can be seen in the printout.