Name: Achmad Fahry Baihaki
NIM: 2206065110100
Institute: Maulana Malik Ibrahim Islamic State University of Malang
Departement: Computer Science
Lecturer: Prof. Dr. Suhartono, M.Kom


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:

1. AND Operator (&)
a <- c(TRUE, TRUE, FALSE)
x <- c(FALSE, TRUE, FALSE)

print(a & x)
## [1] FALSE  TRUE FALSE
2. OR Operator (|)
a <- c(TRUE, TRUE, FALSE)
x <- c(FALSE, TRUE, TRUE)

print(a | x)
## [1] TRUE TRUE TRUE
3. AND Element Wise (&&)
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
4. OR Element Wise (||)
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
5. NOT Operator (!)
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.