Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can match() have a range included in R?

Tags:

r

match

I am trying to use match() in R to find any matching values within a certain interval. For example:

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

gives me all the locations where 2.4 occurs 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!

like image 947
P_A_Logan Avatar asked Mar 16 '23 15:03

P_A_Logan


1 Answers

In that case, I would use subsetting:

v[v>2.2 & v<2.6]

or

which(v>2.2 & v<2.6)

depending on if you want the values or the index

like image 151
jeremycg Avatar answered Mar 18 '23 16:03

jeremycg