title: Calculating distance between two points, but only for a subset of pairs
body: <p>I am trying to calculate distances between pairs of coordinates, which is fairly easy using (for example) the gDistance function in the rgeos package. I have problems figuring out the following twist, however: In my data frame, I have information on points of interest of different types (let's say coffee shops, fast food restaurants, and bars) and I am only interested in the distances between two POIs of different types. </p>
  
  <p>This is what my data frame looks like:</p>
  
  <pre><code>    lat &lt;- c(50.639342, 50.623727, 50.578924, 50.786729)
      lon &lt;- c(10.236543, 10.1896532, 10.587272, 10.776234)
      type &lt;- c("A", "A", "B", "C")
      df &lt;- data.frame(lat, lon, type)
  </code></pre>
  
  <p>I can calculate the distances between each pair by converting the df into a spatial object...</p>
  
  <pre><code>    if (!require(sp)) install.packages('sp')
      library(sp)
      sp.data &lt;- df
      coordinates(sp.data) &lt;- ~lat+lon
  </code></pre>
  
  <p>...and using the gDistance function to obtain a pair wise matrix of distances.</p>
  
  <pre><code>    if (!require(rgeos)) install.packages('rgeos')
      library(rgeos)
      distance &lt;- gDistance(sp.data, byid=T)
      distance
      1          2         3         4
      1 0.00000000 0.04942147 0.3558949 0.5594545
      2 0.04942147 0.00000000 0.4001350 0.6088076
      3 0.35589488 0.40013500 0.0000000 0.2808728
      4 0.55945447 0.60880759 0.2808728 0.0000000
  </code></pre>
  
  <p>What I would like to do next is analyze the distances between two points of different types only. For example, I am interested in what is the closest neighbor to a coffee shop that is not itself a coffee shop. My problem is that I don't know how to work with pair wise data. Ideally, I'd use the type column in the original data frame to assign NAs to all cells containing distances between points of the same type, but I cannot figure out how to do this.</p>