Maas James Dr (MED)
2011-Feb-17 15:36 UTC
[R] does range of values in array include a third value?
I'm using the range command to get the minimum and maximum values of an array as in x <- range(array_y) which gives me two values such as [1] -2 9 I need to be able to test if this range of values includes a third value. For example I'd like to query 1) does the range of -2 to 9 include 3, answer TRUE 2) does the range of -2 to 9 include -6, answer FALSE? All values could be negative or positive. Is there a R function that will test this or do I need to programme it? I have searched but not found one. Thanks J ==============================Dr. Jim Maas University of East Anglia [[alternative HTML version deleted]]
Peter Ehlers
2011-Feb-17 15:56 UTC
[R] does range of values in array include a third value?
On 2011-02-17 07:36, Maas James Dr (MED) wrote:> > I'm using the range command to get the minimum and maximum values of an array as in > > x<- range(array_y) > > which gives me two values such as > > [1] -2 9 > > I need to be able to test if this range of values includes a third value. For example I'd like to query > > 1) does the range of -2 to 9 include 3, answer TRUE > 2) does the range of -2 to 9 include -6, answer FALSE? > > All values could be negative or positive. Is there a R function that will test this or do I need to programme it? I have searched but not found one.Here too you can use findInterval(): findInterval( -2, range(array_y) ) == 1 You may want to set the 'rightmost.closed' argument to TRUE. Peter Ehlers> > Thanks > > J > > ==============================> Dr. Jim Maas > University of East Anglia > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
David Winsemius
2011-Feb-17 15:58 UTC
[R] does range of values in array include a third value?
On Feb 17, 2011, at 10:36 AM, Maas James Dr (MED) wrote:> > I'm using the range command to get the minimum and maximum values of > an array as in > > x <- range(array_y) > > which gives me two values such as > > [1] -2 9 > > I need to be able to test if this range of values includes a third > value. For example I'd like to query > > 1) does the range of -2 to 9 include 3, answer TRUE > 2) does the range of -2 to 9 include -6, answer FALSE? > > All values could be negative or positive. Is there a R function > that will test this or do I need to programme it? I have searched > but not found one.There is a function that handles intervals well, findInterval: > findInterval(0, c(-2, 9)) [1] 1 > findInterval(-3, c(-2, 9)) [1] 0 > findInterval(10, c(-2, 9)) [1] 2 So: isxInRange_y <- function(x, y) findInterval(x, range(y)) == 1 If you want to omit NA's which would otherwise poison the effort, you need to wrap the y argument in na.omit(). -- David.> > Thanks > > J > > ==============================> Dr. Jim Maas > University of East Anglia > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT