# [R] Number in interval

David Winsemius dwinsemius at comcast.net
Mon Jul 18 00:09:49 CEST 2011

```On Jul 17, 2011, at 5:29 PM, jim holtman wrote:

> try this:
>
>> z <- c(-1.4,0.5,4.7)
>> (z >= -3) & (z <= 3)
> [1]  TRUE  TRUE FALSE
>>
>
Another way:

> findInterval(z, c(-3,3)) == 1
[1]  TRUE  TRUE FALSE
> z=c(-50,-1.4,0.5,4.7)

And just to prove to myself that it behaves as I expect with values
below -3:

> findInterval(z, c(-3,3)) == 1
[1] FALSE  TRUE  TRUE FALSE

You may need to check that the method you choose behaves as you expect
at the extremes. findInterval behaves the same way as cut() does in
its default mode. The maximum value matches but the minimum value does
not:

> z=c(-50,-1.4,0.5,4.7, 3, -3)
> findInterval(z, c(-3,3)) == 1
[1] FALSE  TRUE  TRUE FALSE FALSE  TRUE

> cut(seq(-4, 4, by=1), breaks=c(-3,3))
[1] <NA>   <NA>   (-3,3] (-3,3] (-3,3] (-3,3] (-3,3] (-3,3] <NA>
Levels: (-3,3]

Jim's method lets you determine if you want inclusivity at both or
either end.
--
David.

>
> On Sun, Jul 17, 2011 at 10:54 AM, Manuel K. <b8220126 at klzlk.com>
> wrote:
>> Hi all,
>>
>> I have an interval (e.g [-3,3]) and a numeric vector z
>> (-1.4,0.5,4.7). How
>> can I test whether an element in z lies between between -3,3? I
>> particularly
>> need a TRUE/FALSE response.
>>
>> Thanks
>> Manuel
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/Number-in-interval-tp3673537p3673537.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
>
> ______________________________________________
> 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

```