Skip to content
Prev 5904 / 29559 Next

convert point data to count data

On Wed, 10 Jun 2009, Karla Falk wrote:

            
Something like:

library(sp)
data(meuse)
coordinates(meuse) <- c("x", "y")
grid <- spsample(meuse, n=2000, type="regular")
gridded(grid) <- TRUE
# make a SpatialPixels grid to count into
data(meuse.grid)
coordinates(meuse.grid) <- c("x", "y")
gridded(meuse.grid) <- TRUE
pts <- spsample(meuse.grid, n=5000, type="random")
# SpatialPoints sample of points within meusse.grid
o <- overlay(grid, pts)
to <- table(o)
# use table() to do the counting
counts <- rep(as.integer(0), nrow(coordinates(grid)))
counts[as.integer(names(to))] <- to
# assign to an output vector by cell index
grid_df <- SpatialPixelsDataFrame(grid, data=data.frame(counts=counts))
summary(grid_df)
hist(grid_df$counts)
spplot(grid_df)

OK?

Roger