In DataFramesMeta
, why should I wrap every condition within a pair of parentheses? Below is an example dataframe where I want a subset that contains values greater than 1 or is missing
.
d = DataFrame(a = [1, 2, missing], b = ["x", "y", missing]);
Using DataFramesMeta to subset:
@chain d begin
@subset @byrow begin
(:a > 1) | (:a===missing)
end
end
If I don't use parentheses, errors pop up.
@chain d begin
@subset @byrow begin
:a > 1 | :a===missing
end
end
# ERROR: LoadError: TypeError: non-boolean (Missing) used in boolean context
The reason is operator precedence (and is unrelated to DataFramesMeta.jl).
See:
as you can see
2 > 1 | 3 > 4
gets parsed as:2 > (1 | 3) > 4
which is not what you want.However, I would recommend you the following syntax for your case:
or
I personally prefer
coalesce
but it is a matter of taste.Note that
||
as opposed to|
does not require parentheses, but you need to reverse the order of the conditions to take advantage of short circuiting behavior of||
as if you reversed the conditions you would get an error:Finally with
@rsubset
this can be just:(I assume you want
@chain
as this is one of the steps you want to do in the analysis so I keep it)