Skip to content

range checking

2 messages · Robert King, Martin Maechler

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

It seems incredibly messy - is there something better?

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

----
Robert King, Statistics, School of Mathematical & Physical Sciences,
University of Newcastle, Australia
Room V133  ph +61 2 4921 5548
Robert.King@newcastle.edu.au   http://maths.newcastle.edu.au/~rking/

"It's easy to lie with statistics.  It's even easier to lie without them."
	-- Frederick Mosteller
#
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