title: Subset SpatialPolygonsDataFrame
body: <p>I have a <code>SpatialPolygonsDataFrame</code> that I created by reading in a shapefile using <code>readOGR</code> in the <code>rgdal</code> package.  I am attempting to use it to generate a sampling grid using <code>spsample</code> in the <code>sp</code> package to be used for interpolation from survey data collected in the area. However, the SpatialPointsDataFrame encompasses a much larger area than the survey, and as a result, the interpolation is predicting values far away from where any surveys were conducted. The survey data and the shapefile were both projected using the same proj4string.</p>
  
  <p>I would like to subset the SpatialPolygonsDataFrame using the coordinates set by the survey stations, but I'm not sure where the relevant values are stored in the object.</p>
  
  <p>I'm afraid that I can't provide the relevant data as the shapefile is not hosted online.  However, I will borrow some code from Paul Hiemstra's response to this <a href="https://stackoverflow.com/questions/8563334/average-values-of-a-point-dataset-to-a-grid-dataset">post</a> for the Netherlands:</p>
  
  <pre><code>library(ggplot2)
  library(sp)
  library(automap)
  library(rgdal)
  
  #get the spatial data for the Netherlands
  con &lt;- url("http://gadm.org/data/rda/NLD_adm0.RData")
  print(load(con))
  close(con)
  
  class(gadm)
  bbox(gadm)
  &gt;         min      max
  &gt;r1  3.360782  7.29271
  &gt;r2 50.755165 53.55458
  </code></pre>
  
  <p>Let's say that the surveys were conducted in this area:</p>
  
  <pre><code>bbox(surveys)
      &gt;         min      max
      &gt;r1     4.000    7.000
      &gt;r2    51.000   53.000
  </code></pre>
  
  <p>How can I crop out that area of the SpatialPolygonsDataFrame?</p>
  
  <hr>
  
  <p>EDIT: This <a href="https://stackoverflow.com/questions/9825221/r-gis-how-to-subset-a-shapefile-by-a-lat-long-bounding-box">question</a> appears to answer mine.  Apologies for not searching hard enough (although the comments did give me some sense of where to turn with rgeos).  However, <code>gIntersection</code>  causes R to crash...</p>