title: Keep which(..., arr.ind = TRUE) results that connect
body: <p>I am trying to take the results of a <code>which(..., arr.ind = TRUE)</code> function and remove the rows that are not the first to &quot;connect&quot; with one another.</p>
  <p><strong>Examples:</strong></p>
  <pre><code>#example 1      example 2      example 3
     row col        row col        row col
       1   4          2   3          1   3
       2   4          2   4          2   5
       4   5          3   5          3   5
       3   6          2   7          4   6
       4   6          3   7          5   6
       3   7          4   7          6   8
       4   7          5   7          9  10
  # should become (trimmed.mtx)
     row col        row col        row col
       1   4          2   3          1   3
       4   5          3   5          3   5
                      5   7          5   6
                                     6   8
  </code></pre>
  <p>These examples can be read in using:</p>
  <pre><code>example1 &lt;- structure(list(row = c(1L, 2L, 4L, 3L, 4L, 3L, 4L), col = c(4L, 4L, 5L, 6L, 6L, 7L, 7L)), .Names = c(&quot;row&quot;, &quot;col&quot;), class = &quot;data.frame&quot;, row.names = c(NA, -7L))
  example2 &lt;- structure(list(row = c(2L, 2L, 3L, 2L, 3L, 4L, 5L), col = c(3L, 4L, 5L, 7L, 7L, 7L, 7L)), .Names = c(&quot;row&quot;, &quot;col&quot;), class = &quot;data.frame&quot;, row.names = c(NA, -7L))
  example3 &lt;- structure(list(row = c(1L, 2L, 3L, 4L, 5L, 6L, 9L), col = c(3L, 5L, 5L, 6L, 6L, 8L, 10L)), .Names = c(&quot;row&quot;, &quot;col&quot;), class = &quot;data.frame&quot;, row.names = c(NA, -7L))
  </code></pre>
  <p>The purpose of this is to take a dist matrix of Euclidean distances and turn it into a sequence of point-to-point distances that skip distances below a certain threshold. While there may be other ways to solve this problem, I am very interested in figuring out the best way to do this by filtering out rows from the which-matrix.</p>
  <p><strong>Reproducible example of my intended use:</strong></p>
  <pre><code>set.seed(81417) # Aug 14th, 2017
  # Generate fake location data (temporally sequential)
  
  x &lt;- as.matrix(cbind(x = rnorm(10, 10, 3), y =  rnorm(10, 10, 3)))
  
  # Find euclidean point-to-point distances and remove distances that are less than:
  value = 5
  # I attempted to do so by calculating an entire Euclidean distance matrix (dist())
  # and then finding a path from point-to-nearest-point 
  # using distances that are greater than the value 
  d &lt;- as.matrix(dist(x[,c(&quot;x&quot;,&quot;y&quot;)]))
  d[lower.tri(d)] &lt;- 0
  mtx &lt;- which(d &gt; value, arr.ind = T)
  mtx
  
  # Change from EVERY point-to-point distance (mtx) &gt; value
  # to only the &quot;connecting&quot; points that exceed the skipping value
  trimmed.mtx &lt;- {?}
  
  # final result
  cbind(x[unique(c(trimmed.mtx)),],d[trimmed.mtx])
  </code></pre>