title: Counting spatialpoints in gridcells
body: <p>SO-gurues!
  I am trying to count the densities of surviving units in different gridcells.</p>
  
  <p>I have two shapefiles with points from the two survey periods in question (one before and one after the mortality event). What I intend is to see whether  there is a difference in survival rates and link the proportion of survival to any climatic variable obtained from the raster value of the desired grid. In the code snippet below I have created some random raster and shapefiles.</p>
  
  <pre><code>packs = c('raster', 'rgdal', 'spatstat', 'sp' ,'dplyr')
  sapply(packs, FUN = 'require', character.only = TRUE)
  xy &lt;- matrix(rnorm(1024),32,32) #Creating the desired raster
      image(xy)
      rast &lt;- raster(xy)
      extent(rast) &lt;- c(36,37,-3,-2)
      projection(rast) &lt;- CRS("+proj=longlat +datum=WGS84")
  
      points &lt;- runifpoint(n =4000, c(36,37,-3,-2))     # Creating the points
      x &lt;- points$x
      y &lt;- points$y
      values &lt;- c(rep(1, 900), rep(0, 3100))
      xy &lt;- cbind(x, y)
      points &lt;- cbind(x, y, values)
      points &lt;- data.frame(points)
      shp &lt;- SpatialPointsDataFrame(coords = xy, data = data.frame(values) ) # creating shpfiles
      projection(shp) &lt;- CRS("+proj=longlat +datum=WGS84")
      subs &lt;- filter(points, values == 1)
      suxy &lt;- select(subs, x,y)
      shpsub &lt;- SpatialPointsDataFrame(coords = suxy, data = data.frame(subs$values)) # creating shpfiles
      projection(shpsub) &lt;- CRS("+proj=longlat +datum=WGS84")
  </code></pre>
  
  <p>When I attempt to extract the points I use the following lines of code</p>
  
  <pre><code>shp &lt;- spTransform(shp, projection(rast)) # make sure they have same transformation
  shpsub &lt;- spTransform(shpsub, projection(rast))
  XY &lt;- xyFromCell(rast, cell = 1:ncell(rast))
  v &lt;- as.data.frame(rast)                 #Extract values from raster
  XY &lt;- data.frame(XY, v)          # Creating a data frame containing coord., cellno and value
  XY$cell &lt;- c(1:ncell(rast))
  cells &lt;- cellFromXY(rast,shp)             # find which cells the points are in
  cells &lt;- rle(cells)                         # returns a value and a length, fast for counting
  cellsfound &lt;- cellFromXY(rast,shpsub)
  cellsfound &lt;- rle(cellsfound)
  Proportion &lt;- data.frame(cell = cells$values, shp = cells$lengths) 
  test &lt;- data.frame(cell = rep(NA,NROW(Proportion)), shpsub = rep(NA, NROW(Proportion))) 
  test$cell &lt;- c(cellsfound$values, rep(NA, nrow(test) - length(cellsfound$values))) 
  test$shpsub  &lt;- c(cellsfound$lengths, rep(NA, NROW(test) - length(cellsfound$lengths)))
  Proportion &lt;- full_join(Proportion, test, by = "cell")
  test.Proportion &lt;- mutate(Proportion, Proportion = shpsub/shp)    #Calculating Proportion
  XY &lt;- left_join(XY, test.Proportion, by = "cell") # Adding Proportion to coord and cell no.
  XY.m &lt;- summarise(XY, )
  XY &lt;- na.omit(XY) ; XY &lt;- XY[,-4] 
  </code></pre>
  
  <p>As I see it. Using rle() returns the same cells multiple times instead of counting the no of points within each individual cell as was my intention. Can anyone please explain me how to do this in a way that retrieves the information on the number of occurrences in the individual cells?</p>