Skip to contents

For observations without ties, the weighted rank of \(X_i\) among \(X_1, \dots, X_n\) with weights \(w_1, \dots, w_n\) is $$\frac{n}{\sum_{k = 1}^n w_k} \sum_{j = 1}^n w_j 1[X_j \le X_i].$$ Thus, multiplying every weight by the same positive constant does not change the ranks, and unit weights reproduce ordinary ranks. Tied values are handled according to ties_method.

Usage

rank_wtd(x, weights = numeric(), ties_method = "average")

Arguments

x

a numeric vector.

weights

an optional vector of nonnegative weights with the same length as x.

ties_method

how to treat ties; one of "average", "min", "first", or "random", as in rank().

Value

a vector of ranks.

Examples

x <- rnorm(100)
w <- rexp(100)
rank(x)
#>   [1]  35  90  48  40   1  54  17  30   4 100  16  37  80  14  71  56  72  68
#>  [19]   9  82  38  47  52  58  19  88  66  34  76   7  62  13  78   6  55  29
#>  [37]  25  75  86   5  24  89  84  87  11  91  50  46  69  59  26  32  15  28
#>  [55]  93  27  64  33  31  94  60  10  49  42  23   3  97  20  45  63  53   2
#>  [73]  44  57  51  96  99  77  79  39  83  22  73  65  67  85  21  61  92  36
#>  [91]  43  41  18  70  95   8  12  81  74  98
rank_wtd(x, w)
#>   [1]  33.7997312  82.0548262  45.1658217  38.3874390   0.6687812  48.8879612
#>   [7]  17.2018680  27.6014610   3.5198278 100.0000000  15.9698353  35.2818499
#>  [13]  73.7240544  15.5518322  66.4966280  49.8302519  66.8486070  60.1585701
#>  [19]   8.8837516  76.5112502  35.4631267  43.4000646  46.7278934  49.9979290
#>  [25]  18.8347618  81.6150042  58.0571756  32.4920199  70.3694496   6.8028024
#>  [31]  53.4082820  12.6713092  71.1601728   6.1639378  49.4485359  27.0012240
#>  [37]  24.3662712  67.8399859  79.8878066   3.8672994  19.7713315  81.7038993
#>  [43]  78.2046298  79.9130369  11.4856508  83.9746215  45.3947170  40.8619196
#>  [49]  62.9068598  51.3441241  25.9765479  29.0949815  15.9251188  26.3605276
#>  [55]  87.7913180  26.2726465  55.6410196  30.8101195  28.5430329  88.2675036
#>  [61]  52.6820362  11.3848835  45.3607897  39.1796899  19.6691632   2.9880564
#>  [67]  93.4526811  18.9889226  40.6864568  54.2181745  46.8315838   1.9536104
#>  [73]  39.3575172  49.8846718  46.1702989  92.4681482  99.9098482  71.0415809
#>  [79]  73.4702540  37.9714199  76.5727269  19.3896612  67.1411983  57.6355465
#>  [85]  58.9489663  79.5426716  19.1963114  53.0439812  86.6212990  34.2790161
#>  [91]  39.2628711  39.1733689  18.5251743  63.1897062  89.1952884   8.5658647
#>  [97]  11.8314894  75.5978237  67.1603194  96.2752919