not true * not true equals true? - r

I am having some problems in my code because of the following behaviour of R:
> F * F
[1] 0
> !T * !T
[1] TRUE
Can somebody explain me how to avoid this?
I need to multiply some values and get the result of a logical multiplication:
!first * !second * !third
And I would like to get a "True" only if all the values are true.

This is a matter of precedence of the operators. The negation ! has a lower precedence than the multiplication.
A list, in descending order of the operator precedence, is given here. This list can also be displayed with ?Syntax.
If the negations of TRUE are put into braces, one obtains a result that is more intuitively clear:
(!TRUE) * (!TRUE)
[1] 0
In this case, each instance of (!TRUE) is coerced into 0 by the multiplication, and the result is obviously 0.
However, the operation !TRUE * !TRUE is equal to !(TRUE * !TRUE) which in turn is equal to !0, and, hence (with the negation coercing the zero into a logical FALSE), the result is equal to TRUE.
To summarize, paraphrasing the title of your question, one could state:
NOT(TRUE * NOT(TRUE)) equals to TRUE.

Related

How to flip the sign of a number if two conditions are true?

I am looking for a mathematical expression to flip the sign of a number if two other numbers are positive.
This is easy for a single condition x > 0, in which case I'd do num *= sign(x).
But how to do that with two conditions x > 0 and y > 0? This is incorrect:
num *= sign(-x) * sign(-y)
(as it flips the sign also if both x < 0 and y < 0, which I don't want).
I'd come up with
num *= sgn(-x) ** H(y)
and
num *= 1 - 2 * H(x) * H(y)
where H is the Heavyside step function, but while that is brief, it's not explicit.
num *= (-1)^(H(x) * H(y))
is a bit better, but can we go nicer/shorter/more explicit?
This question is not specific to a particular programming language - actually, it's not about programming at all, so using an if is not exactly what I want. I am looking for something like an in-line if for mathematicians, and I thought I'd find people with the best mindsets for this question in a programmers' forum... please tell me I was right ;)
EDIT: As #MarkDickinson pointed out, you can use -sign(min(x, y)):
num *= -1 * sign(min(x, y))
Then:
If both are positive - the result is -1.
If at least one of them is negative - the result is 1.
You can use a ternary operator:
In mathematics, a ternary operation is an n-ary operation with n = 3. A ternary operation on a set A takes any given three elements of A and combines them to form a single element of A. The ternary operator is an operator that takes three arguments. The first argument is a comparison argument, the second is the result upon a true comparison, and the third is the result upon a false comparison.
For example in C++ it would read:
num *= (x>0 && y>0) ? -1 : 1;
which represents (condition) ? if_true : if_false.
This would also work for the special case x and/or y evaluate to 0, since it would not flip the sign. Note sign(0) is usually defined as 0 and that is not the behaviour you said you wanted.

Why is list[1:length(list)-1] okay but list[1length(list)-2] wrong?

a is a vector of string. I get an error if I use something else than -1 to try and get the values of the list.
Using :
a[1:(length(a)-2)]
solved the issue.
a[1:length(a)]
[1] "ADE" "DEZ" "dfeefe"
a[1:length(a)-1]
[1] "ADE" "DEZ"
> a[1:length(a)-2] Error in a[1:length(a) - 2] :
only 0's may be mixed with negative subscripts
Why is it wrong ?
Your problem is one of operator precedence. : has higher precedence than -.
a[1:length(a)-2] means a[(1:length(a))-2] rather than your intended a[1:(length(a)-2)]
In your case, a seems to have length 3, in which case 1:length(a) is the vector 1 2 3 and 1:length(a) - 2 is the vector -1 0 1.
Also, even though a[1:length(a)-1] worked, it didn't work how exactly you might think. What you computed is a[0:2] (with the first index 0 simply ignored) rather than just a[1:2].

Does operator precedence explain the difference in these expressions involving multiplication of a numeric with a negated logical?

I have three expressions, each involving multiplication with a logical or its negation. These logicals and their negation represent indicator variables, so that the expressions are conditionally evaluated:
-2*3*!T + 5*7*T
5*7*T + -2*3*!T
(-2*3*!T) + 5*7*T
I expect the above to produce the same result. However:
> -2*3*!T + 5*7*T
[1] 0 # unexpected!
> 5*7*T + -2*3*!T
[1] 35
> (-2*3*!T) + 5*7*T
[1] 35
I am sure this has something to do with operator precedence and type coercion, but I can't work out how it makes sense to even evaluate !T after the *.
You're exactly right that this is about operator precedence. As ?base::Syntax (which you link above) states, ! has lower precedence than all of the arithmetic operators, so the first expression is equivalent to
(-2*3)*!(T + 5*7*T)
(because the expression containing ! has to be evaluated before the final multiplication can be done) or
-6*!(36) # T coerced to 1 in numeric operations
or
-6*FALSE # non-zero numbers coerced to TRUE in logical operations
or
-6*0 # FALSE coerced to 0 in numeric operations

R: boolean OR and double equal

Say a=1; b=2. Why does (a|b)==1 yield TRUE but (a|b)==2 FALSE? What then is a simple way to return TRUE if either (or both) variable is a match?
If you look at the numeric values that TRUE and FALSE evaluate to, they are 1 and 0 respectively
as.numeric(c(TRUE, FALSE))
#[1] 1 0
| compares two Boolean values.
In this case, (a|b) itself returns TRUE because the numbers are coerced to Boolean values by turning 0 into FALSE, and everything else into TRUE.
From ?base::Logic:
Numeric and complex vectors will be coerced to logical values, with
zero being false and all non-zero values being true. Raw vectors are
handled without any coercion for !, &, | and xor, with these operators
being applied bitwise (so ! is the 1s-complement).
== doesn't work that way, though; it coerces the TRUE into it's numeric form, 1, so 1==2 returns FALSE.
From ?base::Comparison:
If the two arguments are atomic vectors of different types, one is
coerced to the type of the other, the (decreasing) order of precedence
being character, complex, numeric, integer, logical and raw.

What does * means in R?

For instance:
> TRUE * 0.5
0.5
> FALSE * 0.5
0
I don't know if the secret here is the * character itself or the way R encodes logical statements, but I can't understand why the results.
R has a fairly loose type system and rather freely does coercion, hopefully when it is sensible. When coerced to numeric by *, logical values become 0 (FALSE) and 1 (TRUE), your expression gets evaluated with the usual mathematical convention of all values times 0 equal 0, and all values times 1 equal the value. The one exception to that rule in the numeric domain is Inf * 0 returns NaN. Character values have no "destination"-type when composed with "*", so "1"*TRUE throws an error.

Resources