Skip to content

screen if a value is within range

4 messages · Andras Farkas, R. Michael Weylandt, Robert Baer +1 more

#
15 < a & a < 20

Wrap it in a function if you like:

in_interval <- function(x, interval){
   stopifnot(length(interval) == 2L)
   interval[1] < x & x < interval[2]
}

in_interval(a, c(15, 20))

Cheers,
Michael
On Sat, Dec 1, 2012 at 4:50 PM, Andras Farkas <motyocska at yahoo.com> wrote:
#
On 12/1/2012 10:50 AM, Andras Farkas wrote:
a = 16.5
ans = (a >= 15 & a <= 20)
ans
[1] TRUE

Rob
#
On Dec 1, 2012, at 9:55 AM, R. Michael Weylandt wrote:

            
Could just use the findInterval function:

findInterval(x, c(15,20) ) == 1

findInterval returns a numeric vector indicating which bin(s) the  
argument vector element(s) fall(s) into. Items below the lower bound  
get a zero which means if the result is used as an index the there  
will be no item chosen for that value. Items above the maximal  
boundary get a value of n+1 where n is the number of bins. (Very  
useful function.)