When passing only a single vector to the logical and/or operator, the operator negates the argument:
> x = c(F,T,T)
> `&`(x)
[1] TRUE FALSE FALSE
> `|`(x)
[1] TRUE FALSE FALSE
To make the logical operator work as idempotent, one needs to pass a single element vector as the second argument:
> `&`(x,T)
[1] FALSE TRUE TRUE
> `|`(x,F)
[1] FALSE TRUE TRUE
Why do the logical operators negate their argument when there is only one argument passed?
R Logical Operators Logical operators are used to carry out Boolean operations like AND , OR etc. Operators & and | perform element-wise operation producing result having length of the longer operand. But && and || examines only the first element of the operands resulting into a single length logical vector.
The logical negation symbol is used in Boolean algebra to indicate that the truth value of the statement that follows is reversed. The symbol (¬) resembles a dash with a tail or the upper half of a rectangle. The arithmetic subtraction symbol (−) and tilde (~) are also used to indicate logical negation.
The logical NOT ( ! ) operator (logical complement, negation) takes truth to falsity and vice versa. It is typically used with boolean (logical) values.
The logical operator *NOT (or ¬) is used to negate logical variables or constants. Any *NOT operators must be evaluated before the *AND or *OR operators are evaluated.
In this case, the value of argument is false, but as we are using the NOT operator, we will get the trueas a result. This is because the NOT operator will inverse the value of the argument. Case 2: (12 < 20)
Please note that binary operators operate on vectors and matrices as well as scalars. Let’s see more about not in operator in this article. The not-in operator is a logical vector, negating the %in% operators on the same arguments. The not in r is the Negation of the %in% operator.
When both logicals are FALSE in an OR operation, so in the case of FALSE | FALSE, the result is FALSE. Remember, the OR operation is not an exclusive or operation, so TRUE | TRUE equals TRUE as well. With the AND operator, only TRUE & TRUE makes a TRUE, anything else is FALSE.
In this case, the value of argument is false, but as we are using the NOT operator, we will get the trueas a result. This is because the NOT operator will inverse the value of the argument. Case 2: (12 < 20) In this case, we will get the falseas the result because the value of argument is true. xor (Logical XOR operator)
This was modified in R 3.2.1 as a result of a bug report. As you've pointed out, the previous behavior made little sense:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With