Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

r programming

What is the xor() function in R?

Salman Yousaf

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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))
The demo code

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.

RELATED TAGS

r programming

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring