convert point data to count data
On Wed, 10 Jun 2009, Karla Falk wrote:
Hi, I have a data set of points that I would like to convert to grid cell counts. Ultimately I would like to end up with a table that has an x-y coordinate for each grid cell, and the count of the # of points falling in each grid cell. Does anyone know a way to do this in R? I have been playing around with using quadratcounts in spatstat, and the overlay methods in sp, but with both I can't figure out how to get an output table of the cell counts (including counts of zero's) with the corresponding x-y utm of the grid cell. Any help would be greatly appreciated!
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
Thanks, Karla
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no