如何select R中向量的接近值?

How to select the close value in vector in R?

我想根据收敛值在向量中找到组,像这样:

    x <- c(1.001, 1.002, 1.003, 2.0)

然后计算组 [c(1.001, 1.002, 1.003)] 的平均值 < 0.1

    result <- c(1.002,1.002,1.002,2)

谢谢! 嘻嘻

下面是一个也能处理无序向量的解决方案:

x <- c(1.001, 1.002, 2.0, 1.003, 2.01, 3)
o <- order(x)
y <- x[o]
g <- cumsum(c(0, diff(y)) >= 0.1)
res <- tapply(y, g, mean)[as.character(g)]
res[o]
#    0     0     1     0     1     2 
#1.002 1.002 2.005 1.002 2.005 3.000 

编辑:

for example, i use the test data [x <- c(1.1,1.2,1.3,1.4,1.5, 1.6)], i want to get the closed value within 0.3, your code will give one group, all value is 1.35. actually, i want to have two group , one is 1.1,1.2,1.3,1.4, another is 1.5, 1.6. and then calculate the mean value for each group.

这是一个使用 for 循环的快速解决方案。它假定 x 已排序。如果它太慢,用 Rcpp 实现就很简单了。

x <- c(1.1,1.2,1.3,1.4,1.5, 1.6)

d <- 0.3

s <- x[1]

g <- numeric(length(x))
g[1] <- 1

for (i in seq_along(x[-1]) + 1) {
  g[i] <- if (x[i] - s <= d) {
    g[i - 1] 
  } else {
    s <- x[i]
    g[i - 1] + 1
  }
}

tapply(x, g, mean)[as.character(g)]
#   1    1    1    1    2    2 
#1.25 1.25 1.25 1.25 1.55 1.55