I run into this problem all the time. Here’s an example where the vector
x contains two kinds of missing, the
missing singleton and a sentinel value of
julia> x = [1, 99, missing, 99, 2, missing, 1];
julia> xmissing1 = ismissing.(x) .| x .== 99
julia> xmissing2 = ismissing.(x) .| (x .== 99)
julia> xmissing1 == xmissing2
The problem here is that
| has a higher precedence than
==, which I find unexpected. In my experience, it seems that in other languages comparisons have a higher precedence than logical conjunctions. For example, here is the precedence table for R.
Of course, in Julia, comparisons do have a higher precedence than
||, but those are not broadcastable.
Does anyone else get bit by this behavior? Does the higher precedence of
& have anything to do with making vectorized chained comparisons work?
When you write
a = 3 | 4 you expect
a to be
3? That’s at least not what I would expect.
Yes, frequently. I really want to add dot support to
.|| at some point, but it requires front-end lisp hackery that I’ve not had a chance to do.
Um, not sure I follow you. My example used
=. Assignment does have lower precedence than comparison operators, as expected.
To clarify, I never use
& as bitwise operators on numbers, I only use them for vectorized boolean operations. So I have no expectation regarding what
3 | 4 should equal.
Would the vectorized
|| use three-value logic? Because of course right now the scalar versions do not:
julia> missing || true
ERROR: TypeError: non-boolean (Missing) used in boolean context
 top-level scope at REPL:1
No, I would want and expect them to behave exactly like their scalar counterparts.
You are right, however, that this would appear quite frequently with missing data.
Hmmm, well then I’m afraid I wouldn’t be able to use
.&& very often. E.g., in the above example,
x .== 99 contains
missings, so I would still have to use
Until then, one can do something like
(x -> ismissing(x) || x == 99).(x)