DataFox
DataFox

Reputation: 5

Using | and & simultaneously in R (either values can be true, but neither can be false)

I have two variables in a dataset: x and y

I want to subset the dataframe in a way where both can be true, but neither can be false

I've tried:

x == 1 | y == 1 & x != 0 & y !=0

I can grasp there is a problem when phrasing the logic like that, but I can't figure why, exactly

How should I be doing it?

Upvotes: 0

Views: 720

Answers (2)

Gregor Thomas
Gregor Thomas

Reputation: 145785

  • x & y - both must be true;
  • x | y - at least 1 is true (or both);
  • xor(x, y) - exactly one is true (not both);
  • !x & !y - neither is true. (Same as !(x | y).)

That should cover it pretty well.

If you data is binary (1s and 0s), 1 will be treated as TRUE, and 0 will be treated as FALSE, so you don't need to bother with a bunch of ==.

If you combine multiple logical operators, I'd strongly suggest using parentheses to make sure the order of operations/grouping is what you think it is.

m = expand.grid(x = c(0:1,NA), y = c(0:1,NA) )

## Truth Table
#    x   y  x & y  x | y  xor(x, y)  !x & !y
# 1  0   0  FALSE  FALSE      FALSE     TRUE
# 2  1   0  FALSE   TRUE       TRUE    FALSE
# 3 NA   0  FALSE     NA         NA       NA
# 4  0   1  FALSE   TRUE       TRUE    FALSE
# 5  1   1   TRUE   TRUE      FALSE    FALSE
# 6 NA   1     NA   TRUE         NA    FALSE
# 7  0  NA  FALSE     NA         NA       NA
# 8  1  NA     NA   TRUE         NA    FALSE
# 9 NA  NA     NA     NA         NA       NA

Upvotes: 2

IRTFM
IRTFM

Reputation: 263352

Because R interprets infix expressions of equal precedence from left to right, I think that once the first two have been evaluated you will find that the rest are entirely superfluous. The logical OR (|) will allow both to be true. It is not an XOR (so-called “exclusive OR). You should realize that if both x and y are NA that you will get an NA. So you might want to test for that first and decide what you want to occur when that is the case.

Upvotes: 0

Related Questions