[R-sig-Geo] Gridding of data

Roger Bivand Roger.Bivand at nhh.no
Wed Jul 30 13:18:30 CEST 2008


On Wed, 30 Jul 2008, Morten Sickel wrote:

> I have a lot of measurements located using GPS projected to UTM. I would 
> prefer to grid those data, i.e. define a grid with e.g. 200m cell size 
> and calculate the average for each cell. Each cell may contain one, zero 
> or many measurements. Another related thing I would like to do is to 
> count for each grid cell how many measurement that falls within that 
> cell

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

>
> Again, this looks like something that should be fairly easy, if you just 
> know how...
>
> Morten
>
> --
> Morten Sickel
> Norwegian Radiation Protection Authority
> tel (+47) 6716 2551
>
> _______________________________________________
> 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




More information about the R-sig-Geo mailing list