In R, let's say we have the vector area = c(rep(c(26:30), 5), rep(c(500:504), 5), rep(c(550:554), 5), rep(c(76:80), 5)) and another yield = c(1:100) vector.
Now, let's say I want to index like this:
> yield[area==27] [1] 2 7 12 17 22 > yield[area==501] [1] 27 32 37 42 47
No problem, right? But strange things start to happen when I try to index them using c(A, B) . (and even weirder when I try c(min:max) ...)
> yield[area==c(27,501)] [1] 7 17 32 42
I expect, of course, the instances that are present in both other examples, and not just some strange combination of them. This works when I can use the pipe OR statement:
> yield[area==27 | area==501] [1] 2 7 12 17 22 27 32 37 42 47
But what if I work with a range? Say I want to index it in the range c(27:503) ? My real example has a lot more data points and ranges, so it makes sense, please do not offer it to me manually, which essentially means:
yield[area==27 | area==28 | area==29 | ... | area==303 | ... | area==500 | area==501]
There must be a better way ...
r
gakera
source share