I have a dataframe with x,y coordinates. I've sampled 10 x,y coordinates from my dataframe:
y <-c(24.637032,21.569813,21.687,19.677183,24.72438,21.433136,24.637032,22.21065,21.443762,25.273882)
x <-c(-156.02597,-112.00287,-112.00287,-105.27803,-159.47324,-157.858,-156.026,-109.68626,-112.12681,-112.15393)
I've converted these points to a SpatialPoints
objects:
library(sp)
myPoints <- data.frame(x,y)
coordinates(myPoints) <- c('x', 'y')
proj4string(myPoints) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84")
I've used these points to make a SpatialGrid
object:
bb <- bbox(myPoints)
myPointsGrid <- GridTopology(cellcentre.offset = bb[, 1] + (cs/2), cellsize = c(1, 1), cells.dim = ceiling(diff(t(bb))/cs))
myPointsGrid <- SpatialGrid(myPointsGrid, proj4string = "+proj=longlat +ellps=WGS84 +datum=WGS84")
I need two things 1. find which grid cells contain a point 2. Find the number of points in each grid cell. I tried…
over(myPointsGrid, myPoints)
…but this has thrown an error. How can I complete this task?
Best Answer
Assuming that your code is correct (I have given the value 1 for the cs variable), the error it gives is:
According to the help file, this is what the function is waiting as input:
It means that you are giving the wrong order of inputs, first is the point location followed by the layer. This is what you want:
with cs=1