Can match () have a range included in R?

I am trying to use match()in R to find suitable values ​​for a specific interval. For instance:

v <- c(2.2, 2.4, 4.3, 1.3, 4.5, 6.8, 0.9)
match(2.4, v)

gives me all the places where 2.4 is found in v, but what if I wanted to give a range for all possible matches? For example, 2.4 +/- 0.2?

Any help is greatly appreciated, thanks in advance!

+4
source share
2 answers

In this case, I would use a subset:

v[v>2.2 & v<2.6]

or

which(v>2.2 & v<2.6)

depending on whether you want values ​​or index

+5
source

This is another option:

which(findInterval(v, c(-.2, .2) + 2.4) == 1)
[1] 1 2

findInterval(v, c(-.2, .2) + 2.4)gives you 1 1 2 0 2 2 0where 1 means the element is inside the interval, 0 means it to the left, and 2 means to the right.

+2
source

All Articles