Hello guys, I need your help. I have a dataset with x and y coordinates (similar to the `quakes`

dataset, but in my data the coordinates are in meters) and I would like to select points that are distant, for example, 50 meters from each other. What is the best way to make this selection?

In place of `d50 <- which(abs(m[,1] - m[,2]) <= 50)`

, calculate the Euclidian distance

```
set.seed(137)
the_points <- seq(100:900)
draw <- sample(the_points,200)
x <- draw[1:100]
y <- draw[101:200]
m <- matrix(c(x, y), nrow = length(x))
d50 <- which(abs(m[,1] - m[,2]) <= 50)
m[d50,]
#> [,1] [,2]
#> [1,] 124 135
#> [2,] 490 518
#> [3,] 748 733
#> [4,] 737 775
#> [5,] 567 536
#> [6,] 769 778
#> [7,] 143 183
#> [8,] 38 79
#> [9,] 687 731
#> [10,] 224 261
#> [11,] 219 215
#> [12,] 442 441
#> [13,] 643 653
#> [14,] 430 447
#> [15,] 556 523
#> [16,] 271 268
#> [17,] 729 710
```

^{Created on 2020-08-17 by the reprex package (v0.3.0)}

1 Like

Hi @technocrat, thank you for the quick answer. I tried:

```
set.seed(137)
the_points <- seq(100:900)
draw <- sample(the_points,200)
x <- draw[1:100]
y <- draw[101:200]
m <- matrix(c(x, y), nrow = length(x))
ed50 <- which(dist(m) <= 50)
m[ed50,]
```

But I got the error:

`Error in m[ed50, ] : subscript out of bounds`

What am I supposed to do?

Hopefully I havent made a mistake but I think its something like :

```
set.seed(137)
the_points <- seq(100:900)
draw <- sample(the_points,200)
x <- draw[1:100]
y <- draw[101:200]
m <- matrix(c(x, y), nrow = length(x))
md <- dist(m,diag=TRUE)
mdm <- as.matrix(md)
mdm[upper.tri(mdm, diag = TRUE)] <- NA
(found_pairs <- which(mdm<=50, arr.ind = TRUE))
(found_individuals <- found_pairs %>% as.numeric() %>% unique())
(matrix_of_found_i <- m[found_individuals,])
```

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.