R中排序函数的"部分"参数

use*_*seR 5 sorting r

?sort声明partial参数可以是NULL或部分排序的索引向量.

我试过了:

x <- c(1,3,5,2,4,6,7,9,8,10)
sort(x)
## [1]  1  2  3  4  5  6  7  8  9 10
sort(x, partial=5)
## [1]  1  3  4  2  5  6  7  9  8 10
sort(x, partial=2)
## [1]  1  2  5  3  4  6  7  9  8 10
sort(x, partial=4)
## [1]  1  2  3  4  5  6  7  9  8 10
Run Code Online (Sandbox Code Playgroud)

我不确定partial排序向量时的意义.

gag*_*ews 9

作为?sort国家,

如果partial不为NULL,则包含结果元素的索引,这些索引将通过部分排序放置在排序数组中的正确位置.

换句话说,以下断言始终为真:

 stopifnot(sort(x, partial=pt_idx)[pt_idx] == sort(x)[pt_idx])
Run Code Online (Sandbox Code Playgroud)

对于任何xpt_idx,例如

 x <- sample(100) # input vector
 pt_idx <- sample(1:100, 5) # indices for partial arg
Run Code Online (Sandbox Code Playgroud)

此行为与Wikipedia关于部分排序的文章中定义的行为不同.在R sort()的情况下,我们不一定计算k个最小元素.

例如,如果

print(x)
##  [1]  91  85  63  80  71  69  20  39  78  67  32  56  27  79   9  66  88  23  61  75  68  81  21  90  36  84  11   3  42  43
##  [31]  17  97  57  76  55  62  24  82  28  72  25  60  14  93   2 100  98  51  29   5  59  87  44  37  16  34  48   4  49  77
##  [61]  13  95  31  15  70  18  52  58  73   1  45  40   8  30  89  99  41   7  94  47  96  12  35  19  38   6  74  50  86  65
##  [91]  54  46  33  22  26  92  53  10  64  83
Run Code Online (Sandbox Code Playgroud)

pt_idx
## [1]  5 54 58 95  8
Run Code Online (Sandbox Code Playgroud)

然后

sort(x, partial=pt_idx)
##  [1]   1   3   2   4   5   6   7   8  11  12   9  10  13  15  14  16  17  18  23  30  31  27  21  32  36  34  35  19  20  37
## [31]  38  33  29  22  26  25  24  28  39  41  40  42  43  48  46  44  45  47  51  50  52  49  53  54  57  56  55  58  59  60
## [61]  62  64  63  61  65  66  70  72  73  69  68  71  67  79  78  82  75  81  80  77  76  74  89  85  88  87  83  84  86  90
## [91]  92  93  91  94  95  96  97  99 100  98
Run Code Online (Sandbox Code Playgroud)

在这里x[5],x[54]......,x[8]被置于正确的位置 - 我们不能对剩下的元素说什么.HTH.

编辑:部分排序可能会减少排序时间,当然,如果您感兴趣,例如只查找一些订单统计信息.

require(microbenchmark)
x <- rnorm(100000)
microbenchmark(sort(x, partial=1:10)[1:10], sort(x)[1:10])
## Unit: milliseconds
##                           expr       min        lq    median        uq      max neval
##  sort(x, partial = 1:10)[1:10]  2.342806  2.366383  2.393426  3.631734 44.00128   100
##                  sort(x)[1:10] 16.556525 16.645339 16.745489 17.911789 18.13621   100
Run Code Online (Sandbox Code Playgroud)