[R] are arithmetic comparison operators binary?

Martin Maechler maechler at stat.math.ethz.ch
Mon Feb 23 15:57:41 CET 2009

>>>>> "WK" == Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk at idi.ntnu.no>
>>>>>     on Mon, 23 Feb 2009 12:06:32 +0100 writes:

Thank you, Wacek, 
though .. "wrong mailing list" 

    WK> the man page for relational operators (see, e.g., ?'<') says:
    WK> "
    WK> Binary operators which allow the comparison of values in atomic vectors.

    WK> Arguments:

    WK> x, y: atomic vectors, symbols, calls, or other objects for which
    WK> methods have been written.
    WK> "

    WK> it is somewhat surprizing that the following works:

    WK> '<'(1)
    WK> # logical(0)

    WK> '<'()
    WK> # logical(0)

    WK> '<'(1,2,3)
    WK> # TRUE

a bit surprising (sic!), indeed, even for me.
Thanks for your notice and report!

If you'd looked a bit in the sources, you'd seen that they
really are supposed to be binary only.

A very small change in the sources does accomplish this, passes
the standard checks (and I cannot imagine reasonable code that
would have relied on the more lenient behavior), so
this will have changed in one of the next versions of R-devel.

Martin Maechler, ETH Zurich

    WK> what does 'binary' mean here, precisely?  in the first two examples, one
    WK> might suspect that '<' treats the missing arguments as missing values,
    WK> but this would not be coherent with what the man page says:

    WK> "
    WK> Missing values ('NA') and 'NaN' values are regarded as
    WK> non-comparable even to themselves, so comparisons involving them
    WK> will always result in 'NA'.
    WK> "

    WK> i can't find the above explained in the documentation.  typing `<` shows
    WK> that it is a

    WK> function(e1, e2) .Primitive("<")

    WK> how come can/should it work with no complaint on input that does not
    WK> consist of exactly 2 arguments?
    WK> in scheme (which is claimed to have been an inspiration for r), < works
    WK> on an arbitrary number of arguments:

    WK> (<)
    WK> ;; #t
    WK> (< 1)
    WK> ;; #t
    WK> (< 1 2 3)
    WK> ;; #t
    WK> (< 1 2 0)
    WK> ;; #f

    WK> but there < is an arity-dispatched procedure, not a binary one, and it
    WK> produces sensible output for any number of arguments (arguably for n=0, 1).

    WK> vQ

More information about the R-help mailing list