Subset differences between || and | operators

Hi, I’m new to Julia. I have some questions regarding the subset function of DataFrames. I would appreciate if you could help me to solve them.

df = DataFrame(x=[2,0,8,9], y=[0,0,0,8], z=["A", "B", "C", "D"], w=["A","A","B", "B"])

When I filter the dataframe using the || operator, I get an incorrect result. It does not filter correctly the :y column.

subset(df , [:x, :y] => ByRow((a,b)-> a !=0 || b !=0))

But when I use the | operator the filter is correct.

subset(df , [:x, :y] => ByRow((a,b)-> a !=0 | b !=0))

On the other hand, if I run the following code, I get an error because I am not using the || operator.

subset(df , [:z, :w] => ByRow((a,b)-> a =="A" | b == "A" ))
subset(df , [:z, :w] => ByRow((a,b)-> a =="A" || b == "A" ))

So my doubts are:

why in the first case the || operator gives an incorrect result when the variable is numeric. While in the second case, when the variable is a string, it gives a correct result.

I apologize if my English is not understood well.

This doesn’t mean what you think: the | operator has lower precedence than !=, so your expression is equivalent to a != (0 | b) != 0.

And since 0 | b is just b for any integer b, this means that what you have is effectively a != b != 0.

What do you mean? I get:

julia> subset(df , [:x, :y] => ByRow((a,b)-> a !=0 || b !=0))
3Γ—4 DataFrame
 Row β”‚ x      y      z       w      
     β”‚ Int64  Int64  String  String 
─────┼──────────────────────────────
   1 β”‚     2      0  A       A
   2 β”‚     8      0  C       B
   3 β”‚     9      8  D       B

which looks correct to me.

It sounds like what you want is a != 0 && b != 0, not ||?

1 Like

Thank you for your response! You are right, I got confused.