Logical Operators
The following table shows the logical operators supported by R language.
The following table shows the logical operators supported by the R language. It is applicable only to vectors of type logical, numeric, or complex. All numbers greater than 1 are considered as logical value TRUE.
Each element of the first vector is compared with the corresponding element of the second vector. The result of the comparison is a Boolean value.
Operator
Description
Example
&
It is called the element-wise Logical AND operator. It combines each element of the first vector with the corresponding element of the second vector and gives an output TRUE if both the elements are TRUE.
v <- c(3,1,TRUE,2+3i)
t <- c(4,1,FALSE,2+3i)
print(v&t)
it produces the following result −
[1] TRUE TRUE FALSE TRUE
|
It is called the element-wise Logical OR operator. It combines each element of the first vector with the corresponding element of the second vector and gives an output TRUE if one of the elements is TRUE.
v <- c(3,0,TRUE,2+2i)
t <- c(4,0,FALSE,2+3i)
print(v|t)
it produces the following result −
[1] TRUE FALSE TRUE TRUE
!
It is called Logical NOT operator. Takes each element of the vector and gives the opposite logical value.
v <- c(3,0,TRUE,2+2i)
print(!v)
it produces the following result −
[1] FALSE TRUE FALSE FALSE
The logical operator && and || considers only the first element of the vectors and gives a vector of a single element as output.
Operator
Description
Example
&&
Called Logical AND operator. Takes the first element of both the vectors and gives the TRUE only if both are TRUE.
v <- c(3,0,TRUE,2+2i)
t <- c(1,3,TRUE,2+3i)
print(v&&t)
it produces the following result −
[1] TRUE
||
Called Logical OR operator. Takes the first element of both the vectors and gives the TRUE if one of them is TRUE.
v <- c(0,0,TRUE,2+2i)
t <- c(0,3,TRUE,2+3i)
print(v||t)
it produces the following result −
[1] FALSE
Last updated
Was this helpful?