[R] a < b < c is alway TRUE

John Chambers jmc at research.bell-labs.com
Sat Jul 7 15:50:05 CEST 2001

If memory serves, one main reason we allowed (silent) conversion from
logical (FALSE, TRUE) to numeric (0, 1) was for indexing.  Programming
tricks of the form of:

   c("negative", "positive")[ (x >= 0) + 1]

Not that I'm advocating this as good practice, but it provided a compact
way to generate information.  (and, yes, we picked up Fortran (and APL)
style indexing from 1 rather than C style indexing from 0, so we did
need to add the 1)

Converting logical to numeric and then using the result in a comparison
operation, as opposed to an arithmetic operation, seems unlikely to be
what the user intended, on the other hand.  As Duncan Murdoch commented,
putting out a warning for the expression
  a < b < c
would seem a good thing, if not exactly high priority.

Most people with any substantial computing experience would probably
realize that what they meant to say was
  a < b & b < c
If it wasn't and they wanted the warning message to shut up, they would
need to write
 as.numeric( a< b) < c

Yes, we probably should have been less fond of "cute" programming tricks
like the example.  In our defense, though, sometimes they helped people
to program in "whole object" terms, particularly in the days when
interfacing to C and other languages was less flexible.

John M. Chambers                  jmc at bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-2681
700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
Murray Hill, NJ  07974            web: http://www.cs.bell-labs.com/~jmc
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list