Skip to content
Prev 3960 / 29559 Next

Gridding of data

On Wed, 30 Jul 2008, Morten Sickel wrote:

            
The overlay methods in sp for SpatialPoints* and your grid (GridTopology, 
SpatialPixels, SpatialGrid) ought in some combination return a vector of 
length equat to the number of grid cells, with an integer index value. 
Doing tapply() on your SpatialPoints* variable(s) should give output that 
can be inserted into a variable in (probably) a SpatialGridDataFrame:

library(sp)
data(meuse)
data(meuse.grid)
coordinates(meuse) <- c("x", "y")
coordinates(meuse.grid) <- c("x", "y")
gridded(meuse.grid) <- TRUE
o <- overlay(meuse.grid, meuse)
# o is a vector of flattened grid indices, here for the SpatialPixels 
# object
oo <- tapply(meuse$zinc, o, mean)
meuse.grid$zinc <- NA
meuse.grid$zinc[as.integer(names(oo))] <- c(oo)
image(meuse.grid, "zinc")
points(meuse, pch=4)

Do "look" at o and oo as a sanity check, and maybe pass arguments to the 
FUN in tapply too. Perhaps o should be checked for NAs too.

Roger