Operators In R :-
Logical Operators :-
The logical operators supported by R language. It is applicable
only to vectors of type logical, numeric or complex.
Each element of the first vector is compared with the
corresponding element of the second vector. The result of
comparison is a Boolean value.
Table for logical vector :-
Operator Description Example
& It is called Element-wise Logical AND a <- c(FALSE,TRUE,TRUE,FALSE)
operator. It compares each element of the b <- c(TRUE,TRUE,FALSE,FALSE)
Shorter first vector with the corresponding element print(a&b)
AND of the second vector and gives a output
TRUE if both the elements are TRUE. Output :-
Otherwise FALSE. [1] FALSE TRUE FALSE FALSE
| a <- c(FALSE,TRUE,TRUE,FALSE)
It is called Element-wise Logical OR b <- c(TRUE,TRUE,FALSE,FALSE)
operator. It combines each element of the print(a|b)
Shorter first vector with the corresponding element
OR Output :-
of the second vector and gives a output
TRUE if one the elements is TRUE. [1] TRUE TRUE TRUE FALSE
! a <- c(FALSE,TRUE,TRUE,FALSE)
It is called Logical NOT operator. Takes print(!a)
each element of the vector and gives the
Logical Output :-
opposite logical value.
[1] TRUE FALSE FALSE TRUE
NOT
The logical operator && and || considers only the first element of the
vectors and give a vector of single element as output.
Longer AND ( && ) and longer OR ( || )
Operator Description Example
&& a <- c(FALSE,TRUE,TRUE,FALSE)
Called Logical AND operator. Takes first b <- c(TRUE,TRUE,FALSE,FALSE)
Longer element of both the vectors and gives the
AND print(a&&b)
TRUE only if both are TRUE.
output:-
[1] FALSE
|| a <- c(FALSE,TRUE,TRUE,FALSE)
b <- c(TRUE,TRUE,FALSE,FALSE)
Longer Called Logical OR operator. Takes first
OR element of both the vectors and gives the print(a||b)
TRUE if one of them is TRUE.
it produces the following result −
[1] TRUE
THANK YOU