1. In R language, how to find the number that meets the conditions?
For example, given a vector c2, you need to find a number with a value greater than 0:
> c2 [1] 0.00 0.00 0.00 0.00 0.00 0.00 0.06 0.09 0.20 0.09 0.08 0.14 0.14 0.23 [15] 0.08 0.06 0.12 0.20 0.14 0.11 0.20 0.14 0.17 0.15 0.18 0.15 0.20 0.12 [29] 0.23 0.08 0.12 0.08 0.23 0.12 0.08 0.17 0.18 0.17 0.12 0.17 0.14 0.18 [43] 0.11 0.27 0.06 > c2[c2>0] [1] 0.06 0.09 0.20 0.09 0.08 0.14 0.14 0.23 0.08 0.06 0.12 0.20 0.14 0.11 [15] 0.20 0.14 0.17 0.15 0.18 0.15 0.20 0.12 0.23 0.08 0.12 0.08 0.23 0.12 [29] 0.08 0.17 0.18 0.17 0.12 0.17 0.14 0.18 0.11 0.27 0.06 >
2. The numbers that meet the conditions have been found, but how do you get the position (or index) of these numbers in the original vector?
The answer is to use the which() function. First find a sequence that satisfies greater than 0:
> c2>0 [1] FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE [13] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [25] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE [37] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
Secondly, based on the sequence identification, the corresponding index can be found.
> which(c2>0) [1] 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 [25] 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
3. Find and judge
(1) Determine that at least one number meets the conditions
For example, it is determined that at least one number in a vector is less than or equal to zero
> any(c2<=0) [1] TRUE
(2) Determine that all numbers meet the conditions
For example, judge that all numbers are greater than 0, judge that all numbers are greater than or equal to 0:
> all(c2>0) [1] FALSE > all(c2>=0) [1] TRUE >
Supplement: R language - query the data of the specified conditions in the query vector - which
As shown below:
> c(1,2,5,2,1,6,19,77,68,21,2,2,1,1)->x > which(x>20)->y > y [1] 8 9 10 > x[y] [1] 77 68 21
Which function returns the subscript of the element that meets the condition
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.