- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the difference between order and rank function in R?
The rank function gives the rank of the values in a vector if the vector is sorted but in the same sequence as the original vector and the order function gives the position of the original value in the vector but in the sequence of the sorting in ascending order. The rank function is mostly used for ranking when we deal with ordinal variables, hence, we should use it whenever ranking of values is required, on the other hand, order is frequently used for ordering all numerical values.
Examples
set.seed(100) x1<-1:50 order(x1) [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 rank(x1) [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 x2<-sample(1:10,50,replace=TRUE) x2 [1] 10 7 6 3 9 10 7 6 6 4 7 6 2 7 7 7 8 2 3 3 8 2 9 2 3 [26] 4 4 4 5 7 9 4 2 6 7 1 6 9 9 9 6 8 7 1 9 6 4 8 3 4 order(x2) [1] 36 44 13 18 22 24 33 4 19 20 25 49 10 26 27 28 32 47 50 29 3 8 9 12 34 [26] 37 41 46 2 7 11 14 15 16 30 35 43 17 21 42 48 5 23 31 38 39 40 45 1 6 rank(x2) [1] 49.5 33.0 24.5 10.0 45.0 49.5 33.0 24.5 24.5 16.0 33.0 24.5 5.0 33.0 33.0 [16] 33.0 39.5 5.0 10.0 10.0 39.5 5.0 45.0 5.0 10.0 16.0 16.0 16.0 20.0 33.0 [31] 45.0 16.0 5.0 24.5 33.0 1.5 24.5 45.0 45.0 45.0 24.5 39.5 33.0 1.5 45.0 [46] 24.5 16.0 39.5 10.0 16.0 x3<-sample(1:100,50,replace=TRUE) x3 [1] 27 46 59 46 69 47 55 47 68 12 51 16 56 22 82 53 3 5 44 85 28 52 25 42 15 [26] 57 42 76 37 26 24 12 9 55 75 63 35 58 26 48 80 37 94 91 18 72 91 72 66 19 order(x3) [1] 17 18 33 10 32 25 12 45 50 14 31 23 30 39 1 21 37 29 42 24 27 19 2 4 6 [26] 8 40 11 22 16 7 34 13 26 38 3 36 49 9 5 46 48 35 28 41 15 20 44 47 43 rank(x3) [1] 15.0 23.5 36.0 23.5 40.0 25.5 31.5 25.5 39.0 4.5 28.0 7.0 33.0 10.0 46.0 [16] 30.0 1.0 2.0 22.0 47.0 16.0 29.0 12.0 20.5 6.0 34.0 20.5 44.0 18.5 13.5 [31] 11.0 4.5 3.0 31.5 43.0 37.0 17.0 35.0 13.5 27.0 45.0 18.5 50.0 48.5 8.0 [46] 41.5 48.5 41.5 38.0 9.0 x4<-sample(1:1000,50,replace=TRUE) x4 [1] 863 663 844 794 261 759 334 296 879 849 844 222 1000 448 223 [16] 694 170 387 742 336 730 422 728 427 723 900 714 494 818 780 [31] 895 316 813 364 805 490 964 100 201 922 283 927 942 71 661 [46] 551 988 705 272 82 order(x4) [1] 44 50 38 17 39 12 15 5 49 41 8 32 7 20 34 18 22 24 14 36 28 46 45 2 16 [26] 48 27 25 23 21 19 6 30 4 35 33 29 3 11 10 1 9 31 26 40 42 43 37 47 13 rank(x4) [1] 41.0 24.0 38.5 34.0 8.0 32.0 13.0 11.0 42.0 40.0 38.5 6.0 50.0 19.0 7.0 [16] 25.0 4.0 16.0 31.0 14.0 30.0 17.0 29.0 18.0 28.0 44.0 27.0 21.0 37.0 33.0 [31] 43.0 12.0 36.0 15.0 35.0 20.0 48.0 3.0 5.0 45.0 10.0 46.0 47.0 1.0 23.0 [46] 22.0 49.0 26.0 9.0 2.0 x5<-sample(1:1000,100) x5 [1] 555 476 392 954 332 660 567 209 115 793 491 560 682 304 551 220 896 703 [19] 519 553 510 763 351 19 177 354 945 130 637 466 694 56 140 383 292 905 [37] 129 87 111 397 695 811 297 327 619 76 325 573 720 461 224 38 374 904 [55] 685 237 98 163 717 221 903 898 615 960 233 639 824 585 175 947 923 28 [73] 12 107 679 644 119 959 296 752 146 625 359 218 324 401 120 755 909 355 [91] 23 737 991 85 2 406 957 568 268 964 order(x5) [1] 95 73 24 91 72 52 32 46 94 38 57 74 39 9 77 87 37 28 [19] 33 81 58 69 25 8 84 16 60 51 65 56 99 35 79 43 14 85 [37] 47 44 5 23 26 90 83 53 34 3 40 86 96 50 30 2 11 21 [55] 19 15 20 1 12 7 98 48 68 63 45 82 29 66 76 6 75 13 [73] 55 31 41 18 59 49 92 80 88 22 10 42 67 17 62 61 54 36 [91] 89 71 27 70 4 97 78 64 100 93 rank(x5) [1] 58 52 46 95 39 70 60 24 14 83 53 59 72 35 56 26 86 76 [19] 55 57 54 82 40 3 23 41 93 18 67 51 74 7 19 45 32 90 [37] 17 10 13 47 75 84 34 38 65 8 37 62 78 50 28 6 44 89 [55] 73 30 11 21 77 27 88 87 64 98 29 68 85 63 22 94 92 5 [73] 2 12 71 69 15 97 33 80 20 66 43 25 36 48 16 81 91 42 [91] 4 79 100 9 1 49 96 61 31 99
Advertisements