How to find the rank of a vector elements in R from largest to smallest?

To find the rank of a vector of elements we can use rank function directly but this will result in ranks from smallest to largest. For example, if we have a vector x that contains values 1, 2, 3 in this sequence then the rank function will return 1 2 3. But if we want to get ranks from largest to smallest then it would be 3 2 1 and it can be done in R as rank(-x).
 Live Demo
x1<-1:10
x1
[1] 1 2 3 4 5 6 7 8 9 10
rank(-x1)
[1] 10 9 8 7 6 5 4 3 2 1

 Live Demo
x2<-rpois(100,5)
x2
[1]  5 1 4 8 5 1 5 6 2 1 6 5 1 4 2 4 3 6 7 6 3 5 7 4 11
[26] 5 2 4 6 2 6 9 1 1 5 7 2 5 4 3 8 9 5 5 6 4 9 4 2 6
[51] 4 9 5 1 4 9 4 8 6 5 5 6 1 12 7 6 3 5 9 2 6 3 6 4 5
[76] 9 3 4 6 3 6 5 11 2 4 7 2 5 5 7 5 9 4 2 3 6 3 4 5 4
rank(-x2)
[1] 55.5 55.5 36.5 55.5 36.5 23.0 89.0 36.5 74.5 55.5 74.5 1.0
 [13] 74.5 36.5 7.5 89.0 55.5 74.5 36.5 89.0 14.0 36.5 36.5 97.0
[25] 89.0 14.0 55.5 89.0 89.0 14.0 4.0 36.5 74.5 89.0 55.5 97.0
[37] 74.5 23.0 97.0 14.0 36.5 7.5 89.0 23.0 14.0 89.0 23.0 55.5
[49] 55.5 97.0 36.5 2.0 55.5 74.5 74.5 89.0 7.5 4.0 100.0 74.5
[61] 36.5 55.5 55.5 23.0 14.0 36.5 36.5 23.0 36.5 55.5 55.5 36.5
[73] 36.5 74.5 14.0 55.5 14.0 7.5 55.5 55.5 55.5 4.0 23.0 97.0
[85] 74.5 89.0 74.5 74.5 14.0 55.5 55.5 74.5 36.5 36.5 74.5 74.5
[97] 23.0 74.5 23.0 74.5
 Live Demo
x3<-sample(0:9,100,replace=TRUE)
x3
[1] 3 9 0 5 8 3 4 6 0 6 8 2 2 8 7 9 4 2 3 7 4 9 7 8 0 6 3 2 2 4 7 9 0 0 2 8 3
[38] 2 4 9 9 2 3 2 9 1 8 1 3 2 9 2 2 5 6 2 5 8 8 9 7 5 1 8 0 1 3 5 1 3 4 3 9 5
[75] 3 9 9 5 1 1 7 8 6 3 4 4 1 9 9 2 1 6 6 7 4 6 2 5 8 3
rank(-x3)
[1] 80.0 14.5 54.5 71.0 54.5 93.0 45.5 5.5 5.5 64.0 64.0 54.5 14.5 71.0 80.0
[16] 5.5 93.0 54.5 54.5 14.5 54.5 24.0 64.0 14.5 80.0 45.5 80.0 54.5 36.0 14.5
[31] 36.0 80.0 80.0 36.0 64.0 93.0 36.0 71.0 54.5 24.0 5.5 80.0 36.0 93.0 45.5
[46] 24.0 54.5 80.0 93.0 80.0 14.5 93.0 5.5 45.5 54.5 54.5 36.0 45.5 93.0 36.0
[61] 24.0 54.5 5.5 24.0 14.5 71.0 64.0 36.0 80.0 45.5 24.0 5.5 24.0 5.5 93.0
[76] 64.0 24.0 93.0 24.0 24.0 93.0 5.5 93.0 36.0 36.0 71.0 93.0 93.0 64.0 80.0
 [91] 36.0 36.0 5.5 24.0 71.0 71.0 14.5 36.0 93.0 93.0
 Live Demo
x4<-sample(100,100,replace=TRUE)
x4
[1] 24 2 100 26 18 61 25 100 9 22 3 19 74 93 59 80 88 32
[19] 54 35 44 19 60 87 81 16 71 10 75 13 8 54 58 44 56 1
[37] 14 69 55 4 19 90 22 16 73 4 65 21 79 62 10 6 78 29
[55] 25 37 69 77 20 24 52 78 50 81 92 13 21 15 50 14 84 78
[73] 64 94 6 3 58 23 88 85 35 60 8 75 84 45 17 94 42 98
[91] 67 88 29 34 40 47 20 62 75 73
rank(-x4)
[1] 70.0 53.0 87.0 42.5 18.0 8.5 44.5 93.5 16.0 12.0 33.0 82.5
[13] 28.5 78.0 26.0 60.5 79.5 87.0 99.0 33.0 82.5 47.0 17.0 44.5
 [25] 12.0 33.0 58.5 54.0 39.0 64.0 12.0 47.0 87.0 92.0 47.0 98.0
 [37] 15.0 55.0 37.5 81.0 49.5 40.5 23.5 79.5 95.5 21.5 40.5 60.5
[49] 75.0 49.5 76.5 72.0 1.5 90.0 97.0 30.5 35.5 3.0 91.0 7.0
[61] 76.5 27.0 21.5 85.0 4.5 19.5 56.5 19.5 65.0 89.0 37.5 35.5
[73] 25.0 66.0 93.5 72.0 12.0 72.0 12.0 28.5 63.0 56.5 84.0 1.5
 [85] 58.5 6.0 95.5 62.0 8.5 69.0 68.0 67.0 4.5 51.0 100.0 74.0
[97] 42.5 30.5 52.0 23.5
 Live Demo
x5<-sample(101:199,100,replace=TRUE) x5
[1] 184 142 123 176 190 156 148 111 123 175 124 189 196 198 191 154 181 199
[19] 155 193 122 194 179 132 111 151 111 178 124 171 114 168 132 164 105 177
[37] 166 169 136 101 127 150 181 180 109 122 132 126 147 162 141 164 125 163
[55] 177 143 119 177 149 111 182 123 122 163 198 130 135 184 138 107 103 147
[73] 140 135 198 138 188 134 114 186 173 138 161 132 160 176 190 117 161 197
[91] 161 124 116 110 126 119 173 183 155 115
rank(-x5)
[1] 30.0 44.0 42.5 9.0 40.5 12.0 34.0 37.0 27.5 83.0 99.0 63.5
[13] 23.5 16.5 92.0 12.0 25.0 67.0 96.5 48.5 78.0 85.0 18.0 96.5
[25] 15.0 82.0 9.0 37.0 5.0 94.5 39.0 29.0 22.0 52.5 66.0 2.0
[37] 58.0 60.0 14.0 81.0 93.0 23.5 90.0 50.5 37.0 88.5 45.5 55.5
[49] 86.5 27.5 26.0 3.0 9.0 88.5 5.0 5.0 91.0 63.5 84.0 63.5
[61] 94.5 48.5 54.0 69.5 34.0 75.5 71.0 20.0 31.5 42.5 100.0 40.5
[73] 20.0 52.5 79.5 69.5 86.5 75.5 55.5 45.5 98.0 73.5 1.0 59.0
 [85] 57.0 47.0 79.5 73.5 20.0 16.5 7.0 50.5 34.0 63.5 68.0 61.0
[97] 31.5 12.0 77.0 72.0
 Live Demo
x6<-sample(rpois(10,5),100,replace=TRUE) x6
[1] 5 5 4 5 5 4 5 8 4 3 2 2 4 5 8 4 6 2 5 4 4 4 4 5 4 3 6 4 6 8 4 8 7 4 4 5 4
[38] 5 3 6 4 4 2 8 4 7 3 2 4 4 7 5 2 4 5 3 4 2 4 2 2 8 3 5 4 4 5 8 7 5 2 4 6 5
[75] 6 4 2 2 3 5 2 2 3 3 8 7 2 3 3 6 8 2 6 5 2 5 5 5 8 5
rank(-x6)
[1] 29.0 96.0 74.5 29.0 74.5 74.5 7.0 96.0 29.0 74.5 74.5 74.5 74.5 74.5 7.0
[16] 7.0 7.0 29.0 96.0 74.5 74.5 7.0 7.0 96.0 74.5 29.0 96.0 7.0 51.0 74.5
[31] 51.0 29.0 7.0 74.5 29.0 74.5 29.0 29.0 74.5 29.0 51.0 29.0 51.0 51.0 29.0
[46] 29.0 74.5 74.5 29.0 74.5 51.0 74.5 74.5 51.0 74.5 29.0 29.0 51.0 74.5 74.5
[61] 51.0 74.5 74.5 29.0 29.0 74.5 29.0 96.0 96.0 29.0 51.0 74.5 74.5 74.5 96.0
[76] 7.0 29.0 7.0 29.0 74.5 29.0 74.5 29.0 29.0 29.0 7.0 74.5 29.0 51.0 7.0
[91] 74.5 7.0 51.0 29.0 96.0 74.5 29.0 51.0 29.0 29.0
 Live Demo
x7<-round(runif(100,5,10),0)
x7
[1] 5 8 6 9 10 7 7 10 5 9 8 9 7 8 8 6 8 6 6 7 9 6 10 10 8
[26] 8 7 9 9 10 9 8 7 7 6 7 10 6 8 9 7 7 9 6 6 5 5 5 9 9
[51] 6 9 10 7 7 5 9 10 7 7 9 7 6 6 7 8 6 6 9 6 10 9 6 8 7
[76] 7 6 9 7 10 9 7 6 6 6 5 9 10 7 6 9 8 8 7 8 8 8 9 9 8
rank(-x7)
[1] 42.5 78.5 22.5 6.0 96.0 6.0 58.5 42.5 42.5 78.5 78.5 22.5 78.5 78.5 6.0
[16] 78.5 78.5 42.5 22.5 22.5 78.5 58.5 6.0 42.5 22.5 58.5 58.5 6.0 22.5 96.0
 [31] 96.0 42.5 6.0 58.5 78.5 96.0 78.5 22.5 22.5 6.0 58.5 42.5 96.0 22.5 22.5
[46] 58.5 58.5 58.5 58.5 78.5 78.5 22.5 22.5 22.5 6.0 78.5 22.5 6.0 78.5 42.5
 [61] 42.5 58.5 78.5 22.5 6.0 78.5 78.5 42.5 42.5 42.5 78.5 78.5 78.5 42.5 96.0
 [76] 78.5 78.5 96.0 42.5 58.5 78.5 42.5 42.5 42.5 6.0 78.5 22.5 58.5 42.5 22.5
[91] 22.5 22.5 22.5 78.5 96.0 58.5 78.5 22.5 22.5 96.0
 Live Demo
x8<-rexp(50,2.5)
x8
[1] 0.29346231 0.19440966 0.77117130 0.08592004 0.04575112 0.04967382
[7] 0.37039906 0.24414045 0.89440198 0.23974022 1.19025638 0.33477031
[13] 1.11400244 0.37368447 0.29478339 0.03654986 0.11947211 0.08989231
[19] 0.15917572 1.22241385 1.09067800 0.15827342 0.40054308 0.04406150
[25] 0.59635287 0.06558528 0.02868031 0.45926452 0.07033172 0.87111673
[31] 0.57026937 1.14810306 0.37622534 0.23697019 0.13202811 0.59222703
[37] 0.54024297 0.04767550 0.07921466 0.52566672 0.49331287 1.50206460
[43] 0.15669989 0.41877724 0.54670825 0.18044803 0.39152010 0.17849852
[49] 0.31529803 1.40226889
rank(-x8)
[1] 15 43 20 12 9 13 27 40 25 32 28 17 46 18 22 8 26 23 47 1 2 41 4 42 29
[26] 34 38 35 24 14 44 49 16 7 19 45 31 36 21 39 33 48 37 3 50 11 30 10 6 5
 Live Demo
x9<-round(rexp(50,2.5),0)
x9
[1] 0 0 0 2 1 0 1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0
[39] 0 1 0 1 0 0 0 0 0 1 0 0
rank(-x9)
[1] 30.5 6.5 30.5 30.5 30.5 30.5 30.5 30.5 30.5 30.5 30.5 30.5 30.5 6.5 1.5
[16] 30.5 30.5 30.5 30.5 30.5 30.5 6.5 30.5 6.5 1.5 30.5 30.5 30.5 6.5 30.5
[31] 30.5 30.5 30.5 6.5 30.5 30.5 30.5 30.5 6.5 6.5 30.5 30.5 30.5 30.5 30.5
[46] 30.5 30.5 30.5 30.5 30.5
 Live Demo
x10<-round(rnorm(100,45,3),0)
x10
[1] 48 48 43 46 48 44 39 49 44 42 46 46 41 50 50 46 46 40 47 45 54 48 44 45 44
[26] 48 48 49 47 44 43 45 35 45 44 42 42 45 49 39 44 39 50 44 44 41 47 44 43 47
[51] 45 44 37 42 49 43 49 39 50 51 41 47 43 45 46 45 45 45 46 43 45 44 50 47 43
[76] 49 49 41 47 40 46 46 48 44 45 46 48 42 42 48 47 49 44 42 46 46 46 40 43 47
rank(-x10)
[1] 95.5 29.5 80.0 54.0 41.5 41.5 68.5 80.0 54.0 29.5 68.5 41.5
[13] 41.5 6.0 54.0 54.0 29.5 19.0 68.5 68.5 98.5 68.5 98.5 41.5
[25] 6.0 91.0 29.5 68.5 80.0 11.0 6.0 29.5 95.5 91.0 14.0 19.0
[37] 54.0 68.5 29.5 80.0 100.0 86.0 54.0 68.5 29.5 1.0 68.5 68.5
 [49] 54.0 29.5 68.5 19.0 6.0 19.0 91.0 68.5 91.0 68.5 95.5 11.0
[61] 86.0 54.0 54.0 2.5 80.0 11.0 68.5 29.5 41.5 68.5 6.0 54.0
 [73] 11.0 54.0 91.0 86.0 41.5 41.5 11.0 41.5 80.0 95.5 41.5 54.0
[85] 19.0 29.5 41.5 29.5 86.0 19.0 19.0 19.0 68.5 29.5 2.5 19.0
[97] 54.0 86.0 41.5 80.0