title: Counting species occurrence in a grid
body: <p>I have about 500,000 points in <code>R</code>  of occurrence data of a migratory bird species throughout the US. </p>
  
  <p>I am attempting to overlay a grid on these points, and then count the number of occurrences in each grid. Once the counts have been tallied, I then want to reference them to a grid cell ID. </p>
  
  <p>In R, I've used the <code>over()</code> function to just get the points within the range map, which is a shapefile.</p>
  
  <pre><code>#Read in occurrence data
  data=read.csv("data.csv", header=TRUE)
  coordinates(data)=c("LONGITUDE","LATITUDE")
  
  #Get shapefile of the species' range map
  range=readOGR(".",layer="data")
  
  proj4string(data)=proj4string(range)
  
  #Get points within the range map
  inside.range=!is.na(over(data,as(range,"SpatialPolygons")))
  </code></pre>
  
  <p>The above worked exactly as I hoped, but does not address my current problem: how to deal with points that are the type <code>SpatialPointsDataFrame</code>, and a grid that is a raster. Would you recommend polygonizing the raster grid, and using the same method I indicated above? Or would another process be more efficient?</p>