4

I am getting a bit confused by the use of the short and long forms of logical operators in R.

If I have the following values

A <- FALSE
B <- TRUE
X <- 3
Y <- 2

I would like to evaluate NOT(A) OR NOT(B) AND X < Y

I expect FALSE given the parameters

This is the expression I have found to evaluate this in R so it returns FALSE as I expect:

!A & X < Y || !B & X < Y

Can I eliminate the repeated X < Y comparison?

smci
  • 32,567
  • 20
  • 113
  • 146
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184

3 Answers3

5

Do you mean:

> (!A || !B) && X < Y
[1] FALSE

?

NPE
  • 486,780
  • 108
  • 951
  • 1,012
3

short form gives you a vector.
long form gives you a single value. compare:

   x <- c(TRUE, TRUE, FALSE)
   y <- c(TRUE, FALSE, FALSE)


   X && Y
   X & y

   x || y
   x | y
Ricardo Saporta
  • 54,400
  • 17
  • 144
  • 178
1

Another possibility:

!(A * B) && X < Y
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168