R language-Find the number that satisfies the condition and get the index

Source: Internet
Author: User
Tags true true

1. In the R language, how to find the number that satisfies the condition?

For example, given a vector c2. Required to find numbers 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. Find the number that satisfies the condition, but how to 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 more than 0:

> c2>0
[1] False to False false false true true if True
True True True if True True True True True true to true True
True True True if True True True True True True True True
[PNS] True True True to true true True True

Secondly, it is identified by the series. You will be able to find the appropriate index.

> 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 inference (1) infer that at least one number satisfies a condition

For example, there is at least one number in the inferred vector that is less than or equal to zero

> Any (c2<=0)
[1] TRUE

(2) Infer that all the numbers meet the conditions

For example, infer that all the numbers are greater than 0, infer that all the numbers are greater than or equal to 0:

> All (c2>0)
[1] FALSE
> All (c2>=0)
[1] TRUE
>



R language-Find the number that satisfies the condition and get the index

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.