[R] does range of values in array include a third value?
David Winsemius
dwinsemius at comcast.net
Thu Feb 17 16:58:46 CET 2011
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
More information about the R-help
mailing list