Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 检测与另一个元素在一定距离内的数值向量元素_R - Fatal编程技术网

R 检测与另一个元素在一定距离内的数值向量元素

R 检测与另一个元素在一定距离内的数值向量元素,r,R,我有一个数字的test_向量,我想在向量中标记位于另一个元素20以内的任何数字的位置。在下面的例子中,你知道如何标记吗 14550(位置1)和14554(位置6) test\u vector这里有一个可能的解决方案,使用outer x[rowSums(abs(outer(x, x, `-`)) < 20) > 1] ## [1] 14550 14554 x[行和(abs(外部(x,x,`-`))1] ## [1] 14550 14554 或者如果你想知道地点 which(rowS

我有一个数字的
test_向量
,我想在向量中标记位于另一个元素20以内的任何数字的位置。在下面的例子中,你知道如何标记吗

14550(位置1)和14554(位置6)


test\u vector这里有一个可能的解决方案,使用
outer

x[rowSums(abs(outer(x, x, `-`)) < 20) > 1]
## [1] 14550 14554
x[行和(abs(外部(x,x,`-`))<20)>1]
## [1] 14550 14554
或者如果你想知道地点

which(rowSums(abs(outer(x, x, `-`)) < 20) > 1)
## [1] 1 6
which(行和(abs(外部(x,x,`-`))<20)>1)
## [1] 1 6

针对拟议备选方案的一些基准

set.seed(123)
x <- sample(1e6, 1e3)
microbenchmark(Outer = which(rowSums(abs(outer(x, x, `-`)) < 20) > 1),
               Sapply = which(rowSums(abs(sapply(x, `-`, x)) < 20) > 1),
               Vapply = which(rowSums(abs(vapply(x, `-`, x, FUN.VALUE = double(length(x)))) < 20) > 1),
               Dist = which(rowSums(as.matrix(dist(x)) < 20) > 1))

# Unit: milliseconds
#    expr      min       lq     mean    median        uq       max neval cld
#   Outer 16.43502 17.84158 30.22553  18.99517  58.39895  64.28932   100 a  
#  Sapply 24.66530 26.64898 39.44647  27.72899  67.83102  75.59510   100  b 
#  Vapply 15.05799 16.62862 25.52292  17.57840  18.94187  64.09142   100 a  
#    Dist 62.25154 66.00407 95.46239 104.26654 107.21883 150.30602   100   c
set.seed(123)
x 1),
sappy=其中(行和(abs(sappy(x,`-`,x))<20)>1),
Vapply=which(行和(abs)(Vapply(x,`-`,x,FUN.VALUE=double(length(x)))<20)>1),
Dist=其中(行和(如矩阵(Dist(x))<20)>1))
#单位:毫秒
#expr最小lq平均uq最大neval cld
#外部16.43502 17.84158 30.22553 18.99517 58.39895 64.28932 100 a
#赛普利24.6653026.64898 39.44647 27.72899 67.83102 75.59510 100 b
#Vapply 15.05799 16.62862 25.52292 17.57840 18.94187 64.09142 100 a
#地区62.2515466.0040795.46239104.26654107.21883150.30602100 c

与@RHertel讨论后:

w     = which(dist(x) < 20)
pairs = t(combn(length(x),2))

pairs[w, , drop = FALSE]
#      [,1] [,2]
# [1,]    1    6
w=哪个(距离(x)<20)
成对=t(梳(长度x,2))
成对[w,drop=FALSE]
#      [,1] [,2]
# [1,]    1    6

通过这种方式,您可以看到哪些元素对彼此的间距在20个单位以内。如果你只是想要元素索引列表,这是唯一的(c(pairs[w,])

向量应该保持这个序列,还是可以排序?需要保持它的顺序,但是如果它可以排序,然后用于查找位置,那就行了。@Davidernburg已经发布了一个不需要任何排序的解决方案。工作非常好,非常感谢。定时器一启动我就接受。也可以使用
as.matrix(dist(x))
代替
abs(外部(…)
。我想它的计算量大约是原来的一半。@Frank我也这么想,但我一直认为
outer
dist
快得多。我想你甚至有一次发布了一个关于这个的问题,不是吗?虽然它可以在abs上节省一些时间。不知道。听起来很熟悉,是的。无论如何,它应该不会慢一些,但我不知道它的内部结构,也不记得它的时间安排。@RHertel在下面补充道。
w     = which(dist(x) < 20)
pairs = t(combn(length(x),2))

pairs[w, , drop = FALSE]
#      [,1] [,2]
# [1,]    1    6