2

I tried to use the 'or' operator in R. However, it does not behave as expected.

(4|6)==1
#TRUE
#expected: FALSE

(4|6)==6
#FALSE
#expected: TRUE

6 %in% c(4,6)
#TRUE

The latter works as expected.

I will use the list version, but why does the or operator behave like it behaves? If i ask whether a number is 4 or 6, I expect that 4 or 6 give me TRUE. However, only 1 is TRUE.

SDahm
  • 474
  • 2
  • 9
  • 21
  • `(4|6)` returns `TRUE`. From `?TRUE`: "Logical vectors are coerced to integer vectors in contexts where a numerical value is required, with TRUE being mapped to 1L, FALSE to 0L", hence `(4|6)==1` returns `TRUE`. Do you need `4 == 6 | 6 == 6` ? – markus Aug 20 '18 at 09:23

3 Answers3

3

Read what the page help('|') says (my emphasis):

Numeric and complex vectors will be coerced to logical values, with zero being false and all non-zero values being true.

So what happens is that both 4 and 6 are coerced to TRUE, then their disjunction is computed.

Follow these examples.

(4|6)
#[1] TRUE

(0|1)
#[1] TRUE

(0|0)
#[1] FALSE

The last is equivalent to FALSE|FALSE, the only way for a disjunction to give FALSE.

What the question is asking for is the result of

(4|6) == 1
#[1] TRUE

After coercing non-zero 4 and non-zero 6 to the respective logical value, this becomes the same as

(TRUE|TRUE) == 1
#[1] TRUE
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
  • So it works for strings/characters but not for integers/numerics. This is not intuitive. Here an example where it works: grepl("Session(1|2|3|4)", temp) – SDahm Aug 20 '18 at 10:28
  • @SDahm Regular expressions are a completely different problem. They have their own set of rules, see [regex](https://stat.ethz.ch/R-manual/R-devel/library/base/html/regex.html). The `|` is not a logical operator that operates on the logical values `FALSE/TRUE/NA`. It's an *infix operator*. – Rui Barradas Aug 20 '18 at 10:34
2

OR or | is a logical operator. So, it is always better to use it in logical operations. 6 %in% c(4,6) in human language become a question like:

is 6 in the this vector? and the answer is TRUE.

But regarding the (4|6)==6 it is difficult to interpret it. Since the parenthesis has the highest priority, then the question is 4 or 6 and the result is TRUE. and then TRUE == 6 which is false. TRUE is considered as 1 in integers. But TRUE == 6 does not make that much seance, so the result is strange as well.

  1. Make sure that priority of the operations are right
  2. Make sure you use the correct operator for correct operands.
Sal-laS
  • 11,016
  • 25
  • 99
  • 169
1

The return values of the logical operators are TRUE(1) or FALSE(0), so you just can ask if logical operators are true or not. So 4|6 technically asks if there is a value or not. Then it returns TRUE because 4 is a value. Afterwards you compare TRUE==6. With TRUE as value 1 it is not True, so you return FALSE. (see your first expression, there it returns TRUE because TRUE==1 is TRUE)

you have to write (4==6|6==6) if you want to ask if it is 6

mischva11
  • 2,811
  • 3
  • 18
  • 34