Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do logical operators negate their argument when there is only one argument in R?

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?

like image 833
Mert Nuhoglu Avatar asked Jun 20 '15 08:06

Mert Nuhoglu


People also ask

How do logical operators work in R?

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.

What is the logical negation symbol in R?

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.

Which logical operator negates the value of a boolean expression?

The logical NOT ( ! ) operator (logical complement, negation) takes truth to falsity and vice versa. It is typically used with boolean (logical) values.

Which logical operator is used to negate a condition?

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.

What is the value of the argument in the not operator?

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)

What is not in operator in R?

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.

What happens when both logicals are false in an or operation?

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.

What is the result of the not operator?

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)


1 Answers

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:

enter image description here

like image 153
Thomas Avatar answered Nov 15 '22 00:11

Thomas