In R: Indexing vectors by boolean comparison of a value in range: index==c(min : max)

前端 未结 2 1193
夕颜
夕颜 2021-01-12 09:17

In R, let\'s say we have a 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 vector yield = c(1:

相关标签:
2条回答
  • 2021-01-12 09:34

    You want to use %in%. Also notice that c(27:503) and 27:503 yield 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
    
    0 讨论(0)
  • 2021-01-12 09:47

    Why not use subset?

    subset(yield, area > 26 & area < 504) ## for indexes
    subset(area, area > 26 & area < 504) ## for values
    
    0 讨论(0)
提交回复
热议问题