James
James

Reputation: 386

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

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

Upvotes: 3

Views: 8657

Answers (4)

IRTFM
IRTFM

Reputation: 263411

I upvoted @MatthewLundberg's answer because i'm a big fan of findInterval, but think the cut function might be easier to use. As he points out the comparison in findInterval is going to give you left-closed intervals, whereas you want right-closed intervals. Right-closed intervals are what cut delivers by default, except they get labeled by default. You can strip the labels with as.numeric:

 cut(1:10, c(-Inf, 5,7, Inf) )
 [1] (-Inf,5] (-Inf,5] (-Inf,5] (-Inf,5] (-Inf,5] (5,7]    (5,7]    (7, Inf]
 [9] (7, Inf] (7, Inf]
Levels: (-Inf,5] (5,7] (7, Inf]

as.numeric( cut(1:10, c(-Inf, 5,7, Inf) ) )
 [1] 1 1 1 1 1 2 2 3 3 3

> get_inter <- function(vec, cutvec){ as.numeric(cut(vec, breaks=c(-Inf,cutvec,Inf) ) ) }
> get_inter(1:10, c(5,7) )
 [1] 1 1 1 1 1 2 2 3 3 3

Upvotes: 3

Neal Fultz
Neal Fultz

Reputation: 9687

If you know it's integers, %in% is some nice syntax sugar:

R>x <- 1:10
R>x %in% 5:8
 [1] FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE

Upvotes: 1

Matthew Lundberg
Matthew Lundberg

Reputation: 42669

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

Upvotes: 5

hrbrmstr
hrbrmstr

Reputation: 78832

Just use vectorized comparisons:

# generate some repeatable numbers
set.seed(1492)
score <- sample(1:10, 25, replace=TRUE)

# show the numbers
print(score)
[1]  3  3  2  2  1  1  9  6  4  8  7  7  2  6  6  8  2  4  7 10  7  4  2  6  1

# printing the value + compare result just to show that it works
# you can do ifelse((score <= 5 | score > 7), something, somethingelse)

print(data.frame(score=score, tst=(score <= 5 | score > 7)))
   score   tst
1      3  TRUE
2      3  TRUE
3      2  TRUE
4      2  TRUE
5      1  TRUE
6      1  TRUE
7      9  TRUE
8      6 FALSE
9      4  TRUE
10     8  TRUE
11     7 FALSE
12     7 FALSE
13     2  TRUE
14     6 FALSE
15     6 FALSE
16     8  TRUE
17     2  TRUE
18     4  TRUE
19     7 FALSE
20    10  TRUE
21     7 FALSE
22     4  TRUE
23     2  TRUE
24     6 FALSE
25     1  TRUE

Upvotes: 2

Related Questions