title: Choose n most evenly spread points across point dataset in R
body: <p>Given a set of points, I am trying to select a subset of n points that are most evenly distributed across this set of points. In other words, I am trying to thin out the dataset while still evenly sampling across space.</p>
  
  <p>So far, I have the following, but this approach likely won't do well with larger datasets. Maybe there is a more intelligent way to choose the subset of points in the first place...
  The following code randomly chooses a subset of the points, and seeks to minimize the distance between the points within this subset and the points outside of this subset.</p>
  
  <p>Suggestions appreciated!</p>
  
  <pre><code>evenSubset &lt;- function(xy, n) {
  
      bestdist &lt;- NA
      bestSet &lt;- NA
      alldist &lt;- as.matrix(dist(xy))
      diag(alldist) &lt;- NA
      alldist[upper.tri(alldist)] &lt;- NA
      for (i in 1:1000){
          subset &lt;- sample(1:nrow(xy),n)
          subdists &lt;- alldist[subset,-subset]
          distsum &lt;- sum(subdists,na.rm=T)
          if (distsum &lt; bestdist | is.na(bestdist)) {
              bestdist &lt;- distsum
              bestSet &lt;- subset
          }
      }
      return(xy[bestSet,])
  }
  
  xy2 &lt;- evenSubset(xy=cbind(rnorm(1000),rnorm(1000)), n=20)
  plot(xy)
  points(xy2,col='blue',cex=1.5,pch=20)
  </code></pre>