[Rd] range checking

Martin Maechler maechler at stat.math.ethz.ch
Tue May 13 11:09:06 MEST 2003


>>>>> "Robert" == Robert King <robert.king at newcastle.edu.au>
>>>>>     on Tue, 13 May 2003 10:01:49 +1000 (EST) writes:

    Robert> I'm tidying up the gld package at the moment, and
    Robert> the following is my best effort at checking if
    Robert> values are outside the range of the functi (which is
    Robert> [0,1] in this case).

    Robert> It seems incredibly messy - is there something
    Robert> better?

    Robert> outside.range <- !as.logical(((p<1)*(p>0))|(sapply(p,
    Robert>          all.equal,1)"TRUE")|(sapply(p, all.equal, 0)"TRUE"))

why not just

     outside.range <- !all(0 <p & p <1)
or   outside.range <- !all(0 <p) || !all(p <1)
or   outside.range <- any(p < 0) || any(p > 1)
or   outside.range <- any(p < 0 | p > 1)

{using the double && or || is a short cut and may be faster on
 average particularly when the first condition is more probable
 than the fast}

Martin



More information about the R-devel mailing list