R Tutorial R Charts & Graphs R Statistics R Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

R - Logical AND operator



In R, Logical AND operator && combines first elements of two vectors and returns TRUE if both of the elements are TRUE, else returns FALSE. It can be applied on vectors of type logical, numeric or complex. All numbers other than 0 are considered as logical value TRUE.

This operator can also be used to combine conditions and returns True when all conditions are true.

Example: Using with vectors

Consider the example below, where && operators is used with vectors.

v1 <- c(10, 0, TRUE, 1+2i)
v2 <- c(20, -1, FALSE, 5+2i)

#Applying && operator
print(v1 && v2)

The output of the above code will be:

[1] TRUE

Example: Using with matrices

Similarly, the && operators can be used with matrices.

m1 <- matrix(c(10, 0, TRUE, 1+2i), nrow=2)
m2 <- matrix(c(20, -1, FALSE, 5+2i), nrow=2)

#Applying && operator
print(m1 && m2)

The output of the above code will be:

[1] TRUE

Example: Combining conditions

Consider the example below, where && operators is used to combine conditions.

i <- 16

if (i <= 25 && i >= 10) {
  sprintf("%d lies between 10 and 25.", i)
} else {
  sprintf("%d is less than 10.", i)
}

The output of the above code will be:

[1] "16 lies between 10 and 25."

❮ R - Operators

5