r-在向量中找到两个最接近的值

时间:2013-05-08 16:49:15

标签: r match

我试图在以下向量中找到两个值,它们接近10.预期值为10.12099196和10.63054170。您的意见将受到赞赏。

 [1]  0.98799517  1.09055728  1.20383713  1.32927166  1.46857509  1.62380423  1.79743107  1.99241551  2.21226576  2.46106916  2.74346924  3.06455219  3.42958354  3.84350238  4.31005838
[16]  4.83051356  5.40199462  6.01590035  6.65715769  7.30532785  7.93823621  8.53773241  9.09570538  9.61755743 10.12099196 10.63018180 11.16783243 11.74870531 12.37719092 13.04922392
[31] 13.75661322 14.49087793 15.24414627 16.00601247 16.75709565 17.46236358 18.06882072 18.51050094 18.71908344 18.63563523 18.22123225 17.46709279 16.40246292 15.09417699 13.63404124
[46] 12.11854915 10.63054170  9.22947285  7.95056000  6.80923943  5.80717982  4.93764782  4.18947450  3.54966795  3.00499094  2.54283599  2.15165780  1.82114213  1.54222565  1.30703661
[61]  1.10879707  0.94170986  0.80084308  0.68201911  0.58171175  0.49695298  0.42525021  0.36451350  0.31299262  0.26922281  0.23197860  0.20023468  0.17313291  0.14995459  0.13009730
[76]  0.11305559  0.09840485  0.08578789  0.07490387  0.06549894  0.05735864 

3 个答案:

答案 0 :(得分:2)

如果不使用sort,我想不出办法。但是,您可以使用partial sort

加快速度
x[abs(x-10) %in% sort(abs(x-10), partial=1:2)[1:2]]
# [1]  9.617557 10.120992

如果多次出现相同的值,您可以在此处获取所有值。因此,您可以使用unique打包,也可以使用match,如下所示:

x[match(sort(abs(x-10), partial=1:2)[1:2], abs(x-10))]
# [1] 10.120992  9.617557

输出输出:

dput(x)
c(0.98799517, 1.09055728, 1.20383713, 1.32927166, 1.46857509, 
1.62380423, 1.79743107, 1.99241551, 2.21226576, 2.46106916, 2.74346924, 
3.06455219, 3.42958354, 3.84350238, 4.31005838, 4.83051356, 5.40199462, 
6.01590035, 6.65715769, 7.30532785, 7.93823621, 8.53773241, 9.09570538, 
9.61755743, 10.12099196, 10.6301818, 11.16783243, 11.74870531, 
12.37719092, 13.04922392, 13.75661322, 14.49087793, 15.24414627, 
16.00601247, 16.75709565, 17.46236358, 18.06882072, 18.51050094, 
18.71908344, 18.63563523, 18.22123225, 17.46709279, 16.40246292, 
15.09417699, 13.63404124, 12.11854915, 10.6305417, 9.22947285, 
7.95056, 6.80923943, 5.80717982, 4.93764782, 4.1894745, 3.54966795, 
3.00499094, 2.54283599, 2.1516578, 1.82114213, 1.54222565, 1.30703661, 
1.10879707, 0.94170986, 0.80084308, 0.68201911, 0.58171175, 0.49695298, 
0.42525021, 0.3645135, 0.31299262, 0.26922281, 0.2319786, 0.20023468, 
0.17313291, 0.14995459, 0.1300973, 0.11305559, 0.09840485, 0.08578789, 
0.07490387, 0.06549894, 0.05735864)

答案 1 :(得分:2)

另一个替代方案是允许用户控制“容差”以设置“接近度”,这可以通过使用一个简单的函数来完成:

close <- function(x, value, tol=NULL){
  if(!is.null(tol)){
    x[abs(x-10) <= tol]
  } else {
    x[order(abs(x-10))]
  }
}

x是值的向量,value是亲密度的比较值,tol是合乎逻辑的,如果是NULL则返回所有“关闭” “按”紧密度“排序到value的值,否则只返回符合tol中给出的条件的值。

> close(x, value=10, tol=.7)
[1]  9.617557 10.120992 10.630182 10.630542


> close(x, value=10)
 [1] 10.12099196  9.61755743 10.63018180 10.63054170  9.22947285  9.09570538 11.16783243
 [8]  8.53773241 11.74870531  7.95056000  7.93823621 12.11854915 12.37719092  7.30532785
[15] 13.04922392  6.80923943  6.65715769 13.63404124 13.75661322  6.01590035  5.80717982
[22] 14.49087793  5.40199462  4.93764782 15.09417699  4.83051356 15.24414627  4.31005838
[29]  4.18947450 16.00601247  3.84350238 16.40246292  3.54966795  3.42958354 16.75709565
[36]  3.06455219  3.00499094  2.74346924  2.54283599 17.46236358 17.46709279  2.46106916
[43]  2.21226576  2.15165780  1.99241551 18.06882072  1.82114213  1.79743107 18.22123225
[50]  1.62380423  1.54222565 18.51050094  1.46857509 18.63563523  1.32927166  1.30703661
[57] 18.71908344  1.20383713  1.10879707  1.09055728  0.98799517  0.94170986  0.80084308
[64]  0.68201911  0.58171175  0.49695298  0.42525021  0.36451350  0.31299262  0.26922281
[71]  0.23197860  0.20023468  0.17313291  0.14995459  0.13009730  0.11305559  0.09840485
[78]  0.08578789  0.07490387  0.06549894  0.05735864

在第一个例子中,我将“亲密度”定义为valuex中每个元素之间的差异最多为0.7。在第二个示例中,函数close返回值向量,其中第一个最接近value中给出的值,而持续时间是来自value的最远值。

由于我的解决方案没有像@Arun所指出的那样提供一种简单(实用)的方法来查找tol,找到最接近的值的一种方法是设置tol=NULL并询问确切的数字接近的值如下:

> close(x, value=10)[1:3]
[1] 10.120992  9.617557 10.630182

这显示x中最接近10的三个值。

答案 2 :(得分:1)

我不确定你的问题是否清楚,所以这是另一种方法。要找到最接近第一个所需值的值10.12099196,请从矢量中减去该值,取绝对值,然后找到最接近元素的索引。明确的:

delx <- abs( 10.12099196 - x)
min.index <- which.min(delx)  #returns index of first minimum if there are duplicates
x[min.index]   #gets you the value itself

道歉,如果这不是你问题的意图。