Finding which element of a vector is between two values in R

Viewed 35436

I have two vectors x and y. I would like to find which elements of x are between the two elements of vector y. How can I do it in R?

x = c( .2, .4, 2.1, 5.3, 6.7, 10.5)
y = c( 1, 7)

I have written the following code, but it does not give me the correct result.

> x = x[ x >= y[1] && x <= y[2]]
> x
numeric(0)

Result should be like this:

res = c(2.1, 5.3, 6.7)
3 Answers
Related