What is the xor() function in R?

Overview

In R, the xor() function is used to evaluate exclusive-OR between x and y argument values. Exclusive-OR or shortly XOR is a boolean logical operation. It takes two boolean input values and returns TRUE or FALSE. If input values are the same, it returns FALSE, otherwise, it returns TRUE.

Exclusive-OR Truth Table

X

Y

X⊕Y

0

0

0

0

1

1

1

0

1

1

1

0

Syntax


xor(x, y)

Parameters

It takes the following argument values:

  • x: First logical value
  • y: Second logical value

Return value

It returns exclusive-OR between argument values x and y and return the result as a logical type.

Example

In this code snippet, we are heading to create two logical vectors and calculate xor between them:

# Creating two r vectors named x & y
x <- c(FALSE, FALSE, TRUE, TRUE)
y <- c(FALSE,TRUE, FALSE, TRUE)
# Invoking xor() function to computer
# exclusive-or between two vectors x & y
cat(xor(x,y))

Explanation

  • Line 2: We initiate the x variable as a logical vector.
  • Line 3: We initiate y variable as a logical vector.
  • Line 6: We invoke xor() to calculate exclusive-OR between the argument values.

Free Resources