Between two numbers in R i.e. 5<=R>7

r

Solution

`findInterval` is almost what you want, but has open right sides to the intervals. Inverting by negating everything in sight gives closed-right-side intervals.

Your code:

x <- function(score) ifelse(score<=5,1,ifelse(score<=7,2,3))

A `findInterval` approach:

y <- function(score) 3 - findInterval(-score, -c(7,5))

Results:

> x(1:20)
 [1] 1 1 1 1 1 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3
> y(1:20)
 [1] 1 1 1 1 1 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3

Problem

am just trying to find a way to identify numbers in a data set that fall between two values. What i have done so far is to use ifelse i.e. ``` ifelse(score<=5,1,ifelse(score<=7,2,3)) ``` and this has worked but I want to know if you guys know a better method of finding say 5<=R>7, thanks James

Original source