title: Match rows in a dataframe by range of values in two columns
body: <p>I have a data frame that looks like this: </p>
  
  <pre><code>SITE_ID &lt;- c("A", "B", "C", "D")
  LAT &lt;- c(450799, 450824, 450825, 450825)
  LONG &lt;- c(6513879, 6513873, 6513873, 6513874)
  YEAR &lt;- c(2006, 2006, 2007, 2008)
  data &lt;- data.frame(SITE_ID, LAT, LONG, YEAR)
  </code></pre>
  
  <p>Where a series of sites were sampled each year over a series of years and their UTM coordinates recorded, but the site names are not consistent from year to year. I want to search through the data frame and figure out how many of the sites (rows) are likely the same site from another year, using the criteria that <strong>both</strong> the latitude and longitude for a site should be within 2 UTM units of each other in order to be considered the same site. For example, in the data frame above, Site_ID B, C, and D should all be considered the same site. So for each row, I would then want a new name, for example, a new column that had a corrected Site_ID name (in the example above, sites B, C, D would be renamed site 1 or some such). </p>
  
  <p>I have tried using a for loop where I looked at the difference between both latitude and longitude between 2 rows but had issues with the indexing. I'd like to know how to make this loop check every row against every other row and stop at length(data[,1]). </p>
  
  <pre><code>count &lt;- 0
  for (i in 1:length(data[,1])) {
    for (j in 1:length(data[,1])) {
      if (abs(data$LAT[i] - data$LAT[i+1]) &lt; 
            2 &amp; abs(data$LONG[i] - data$LONG[i+1]) &lt; 2)
        count &lt;- count + 1
      print(rownames(data[i]))
    }
  }
  </code></pre>
  
  <p>I also tried using the sqldf() package but that seems equivalent to subset(). Thanks for any insight. </p>