R language-Find the maximum 10 numbers in a vector or matrix

Source: Internet
Author: User

First, Vector

The index (position) of the maximum number of 10, can be sorted in descending order, get the index number, and then the first 10 out.
Recommended method:
Order (x,decreasing=true) [1:10]
Detailed process:
1. Test Data X

> x
[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
[0.11] 0.27 0.06

2. Sort by descending order

> Order (x,decreasing=true)
[1] 44 14 29 33 9 18 21 27 25 37 42 23 36 38 40 24 26 12 13 19 22 41 17 28
[25] 31 34 39 20 43 8 10 11 15 30 32 35 7 16 45 1 2 3 4 5 6
>

3. The index of the maximum 10 numbers can be taken out here.
> order (x,decreasing=true) [1:10]
[1] 9 in all

4, you can see, this takes out the 10 number of the index is not pointing to the largest 10 number.
> X[order (x,decreasing=true) [1:10]]
[1] 0.27 0.23 0.23 0.23 0.20 0.20 0.20 0.20 0.18 0.18

Second, The Matrix

First set the matrix y,9 row 5 column, the maximum 10 number of indexes.

> y

[, 1] [, 2] [, 3] [, 4] [, 5]
[1,] 0.00 0.09 0.14 0.12 0.18
[2,] 0.00 0.08 0.11 0.23 0.17
[3,] 0.00 0.14 0.20 0.08 0.12
[4,] 0.00 0.14 0.14 0.12 0.17
[5,] 0.00 0.23 0.17 0.08 0.14
[6,] 0.00 0.08 0.15 0.23 0.18
[7,] 0.06 0.06 0.18 0.12 0.11
[8,] 0.09 0.12 0.15 0.08 0.27
[9,] 0.20 0.20 0.20 0.17 0.06

Answer method:

1. Use Sore.list () to sort> Arrayind (Sort.list (y,decreasing=t) [1:10],dim (y))
[, 1] [, 2]
[1,] 8 5
[2,] 5 2
[3,] 2 4
[4,] 6 4
[5,] 9 1
[6,] 9 2
[7,] 3 3
[8,] 9 3
[9,] 7 3
[Ten,] 1 5


2. Sort using the order () function

The wrong way:

> Arrayind (which (order (y, decreasing = TRUE) <=), Dim (Y))
[, 1] [, 2]
[1,] 5 1
[2,] 3 4
[3,] 4 4
[4,] 1 5
[5,] 4 5
[6,] 5 5
[7,] 6 5
[8,] 7 5
[9,] 8 5
[10,] 9 5

Which (y, decreasing = TRUE) <= 10) means that the data is sorted first, then the index is less than or equal to 10, the maximum number of 10 is after the order, the index should not be <=10, but the first 10 bits is the largest 10 number index.
The right way:
> Arrayind (Order (y,decreasing=true) [1:10],dim (y))
[, 1] [, 2]
[1,] 8 5
[2,] 5 2
[3,] 2 4
[4,] 6 4
[5,] 9 1
[6,] 9 2
[7,] 3 3
[8,] 9 3
[9,] 7 3
[Ten,] 1 5

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

R language-Find the maximum 10 numbers in a vector or matrix

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.