title: Tagging adjacent points in a matrix
body: <p>I have an 8x8 matrix:</p>
  
  <pre><code>1 1 0 0 0 0 0 0
  1 0 0 0 0 0 0 0
  0 0 1 1 0 0 0 0
  0 0 0 0 0 0 0 0
  0 0 0 0 0 1 0 0
  0 0 0 0 0 1 0 0
  0 0 0 1 0 0 0 0
  0 0 0 0 0 0 0 1
  </code></pre>
  
  <p>Code to create it:</p>
  
  <pre><code>examplemat &lt;- matrix(c(1, 1, rep(0, 6), 1, rep(0, 9), 1, 1, rep(0, 17), 1, rep(0, 7), 1, rep(0, 5), 1, rep(0, 11), 1), 8, 8, byrow=T)
  </code></pre>
  
  <p>The ones have been extracted as coordinates:</p>
  
  <pre><code>onecoords &lt;- which(examplemat == 1, arr.ind=T)
  
        row col
   [1,]   1   1
   [2,]   2   1
   [3,]   1   2
   [4,]   3   3
   [5,]   3   4
   [6,]   7   4
   [7,]   5   6
   [8,]   6   6
   [9,]   8   8
  </code></pre>
  
  <p>I need a simple, vectorized approach to clustering the ones into groups of adjacent coordinates. (For the purposes of this task, I would like 4-way up/down/left/right adjacency, but you may also want the option of 8-way adjacency including the diagonals.)</p>
  
  <p>In the example, we would end up with 5 clusters of cells:</p>
  
  <pre><code>      row col clus
   [1,]   1   1    A
   [2,]   2   1    A
   [3,]   1   2    A
   [4,]   3   3    B
   [5,]   3   4    B
   [6,]   7   4    C
   [7,]   5   6    D
   [8,]   6   6    D
   [9,]   8   8    E
  </code></pre>
  
  <p>The 4-way adjacency check is pretty simple: <code>sum(abs(onecoords[1,] - onecoords[2,])) == 1</code> But I'm struggling to work out how to vectorize this efficiently.</p>