Skip to content
Prev 13470 / 29559 Next

Extract values with over()

On 11/21/2011 09:24 PM, Paulo Justiniano Ribeiro Jr wrote:
Paulo, that is a step in the right direction, as
1 2 3 4 5
2 1 3 1 2

gives frequencies for each cell, but this would also be obtained by the
grid itself:
1 2 3 4 5
2 1 3 1 2

However, as
[1] 298
[1] 400

shows we have empty grid cells. but this gives gaps (implicit 0 values)
for empty cells in grid. The simpler alternative is to do the over with
returnList=TRUE, which returns ALL points in each (grid cell, now
polygon) in ap3:
[[1]]
[1] 416 459

[[2]]
[1] 385

[[3]]
[1]  17 132 351

[[4]]
[1] 387

[[5]]
[1] 311 322

taking the length of each vector gives the frequencies (INCLUDING the
zero frequencies that table does NOT give you):

n = sapply(over(ap3, pontos, returnList=TRUE), length)
x = SpatialPixelsDataFrame(grid, data.frame(n=n))
spplot(x, sp.layout=list("sp.points", pontos), at=0:6-.5)

Without doubt, there are other ways of doing this, for instance with
package raster.

Thanks for helping. It seems to be hard to make complex things simple,
and I'd be happy to hear suggestions for making this even simpler.