In R: indexing vectors by logically comparing values ​​in the range: index == c (min: max)

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 ...

+7
r
source share
2 answers

You want to use %in% . Also note that c(27:503) and 27:503 give the same object.

 > yield[area %in% 27:503] [1] 2 3 4 5 7 8 9 10 12 13 14 15 17 [14] 18 19 20 22 23 24 25 26 27 28 29 31 32 [27] 33 34 36 37 38 39 41 42 43 44 46 47 48 [40] 49 76 77 78 79 80 81 82 83 84 85 86 87 [53] 88 89 90 91 92 93 94 95 96 97 98 99 100 
+6
source share

Why not use a subset?

 subset(yield, area > 26 & area < 504) ## for indexes subset(area, area > 26 & area < 504) ## for values 
+1
source share

All Articles