[R] Multiple comparisons: its a trap!

From: Barry Rowlingson <B.Rowlingson_at_lancaster.ac.uk>
Date: Tue 20 Jul 2004 - 19:08:21 EST

Liaw, Andy wrote:
> Stupid me: fell into this trap:
>
>

>>0 == 0 == 0

>
> [1] FALSE
>

  Ouch!

  Python's comparison operators don't have this trap, since they unravel each comparison pair in a chain so that:

   (A op1 B op2 C)

becomes:

   (A op1 B) and (B op2 C)

If you want:

  (A op1 B) op2 C

you have to put the parens in, and that makes you remember there's some Boolean arithmetic going on in there.

This is a nice feature, since we all are used to reading expressions like 2 < X < 10, and you can write them like that in Python, and they mean what they look like.

You can write like that in R, but beware, its not just 0 == 0 == 0 that opens the trap:

  > X = 5
  > 10 < X < 0
  [1] FALSE
  > 0 > X > 10
  [1] TRUE   Of course old hand Fortran programmers understand all this since the second thing they learnt (after learning how to tap the space bar six times) was the order of precedence of operators...

Baz

PS oh, and in Perl (0 == 0 == 0) is a syntax error!



R-help@stat.math.ethz.ch mailing list

https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Jul 20 19:19:35 2004

This archive was generated by hypermail 2.1.8 : Fri 18 Mar 2005 - 02:37:09 EST