Logical Operators

Logical Operators in R simulate element-wise decision operations, based on the specified operator between the operands, which are then evaluated to either a True or False boolean value. Any non-zero integer value is considered as a TRUE value, be it a complex or real number. 

Element-wise Logical AND operator (&)

Returns True if both the operands are True.

R
list1 <- c(TRUE, 0.1)
list2 <- c(0,4+3i)
print(list1 & list2)
Output : FALSE   TRUE
Any non zero integer value is considered as a TRUE value, be it complex or real number. 

Element-wise Logical OR operator (|)

Returns True if either of the operands is True.

R
list1 <- c(TRUE, 0.1)
list2 <- c(0,4+3i)
print(list1|list2)
Output : TRUE  TRUE

NOT operator (!)

A unary operator that negates the status of the elements of the operand.

R
list1 <- c(0,FALSE)
print(!list1)
Output : TRUE  TRUE

Logical AND operator (&&)

Returns True if both the first elements of the operands are True.

R
list1 <- c(TRUE, 0.1)
list2 <- c(0,4+3i)
print(list1[1] && list2[1])
Output : FALSE 
Compares just the first elements of both the lists.

Logical OR operator (||)

Returns True if either of the first elements of the operands is True.

R
list1 <- c(TRUE, 0.1)
list2 <- c(0,4+3i)
print(list1[1]||list2[1])
Output : TRUE 

The following R code illustrates the usage of all Logical Operators in R:  

R
# R program to illustrate 
# the use of Logical operators
vec1 <- c(0,2)
vec2 <- c(TRUE,FALSE)

# Performing operations on Operands
cat ("Element wise AND :", vec1 & vec2, "\n")
cat ("Element wise OR :", vec1 | vec2, "\n")
cat ("Logical AND :", vec1[1] && vec2[1], "\n")
cat ("Logical OR :", vec1[1] || vec2[1], "\n")
cat ("Negation :", !vec1)

Output 

Element wise AND : FALSE FALSE 
Element wise OR : TRUE TRUE 
Logical AND : FALSE 
Logical OR : TRUE 
Negation : TRUE FALSE

R Operators

Operators are the symbols directing the compiler to perform various kinds of operations between the operands. Operators simulate the various mathematical, logical, and decision operations performed on a set of Complex Numbers, Integers, and Numericals as input operands. 

Similar Reads

R Operators

R supports majorly four kinds of binary operators between a set of operands. In this article, we will see various types of operators in R Programming language and their usage....

Arithmetic Operators

Arithmetic Operators modulo using the specified operator between operands, which may be either scalar values, complex numbers, or vectors. The R operators are performed element-wise at the corresponding positions of the vectors....

Logical Operators

Logical Operators in R simulate element-wise decision operations, based on the specified operator between the operands, which are then evaluated to either a True or False boolean value. Any non-zero integer value is considered as a TRUE value, be it a complex or real number....

Relational Operators

The Relational Operators in R carry out comparison operations between the corresponding elements of the operands. Returns a boolean TRUE value if the first operand satisfies the relation compared to the second. A TRUE value is always considered to be greater than the FALSE....

Assignment Operators

Assignment Operators in R are used to assigning values to various data objects in R. The objects may be integers, vectors, or functions. These values are then stored by the assigned variable names. There are two kinds of assignment operators: Left and Right...

Miscellaneous Operators

Miscellaneous Operator are the mixed operators in R that simulate the printing of sequences and assignment of vectors, either left or right-handed....

Contact Us