[R-sig-Geo] stratified random sampling

Heuvelink, Gerard Gerard.Heuvelink at wur.nl
Tue Feb 24 19:43:28 CET 2009


Dear list,
 
The stratified random sampling problem that I submitted a few days ago has already been solved, with the help of several of you, notably Edzer Pebesma. Edzer came up with the following solution:
 
library(sp)
library(rgdal)
nc1 <- readShapePoly(system.file("shapes/sids.shp",package="maptools")[1], proj4string=CRS("+proj=longlat +datum=NAD27"))
pts = do.call(rbind, sapply(slot(nc1, "polygons"), spsample, n=1, type="random"))
plot(nc1)
points(pts, col='blue', pch=19, cex=1)
 
As it happened, the do.call statement did not work in my case (Edzer and Roger may look into why it does not work with all shapes) and had to be replaced by:
 
for (i in 1:length(slot(nc1, "polygons"))) {
    pt = spsample(nc1[i,], n=1, type="random")
    if (i == 1)
        pts = pt
    else
        pts = rbind(pts, pt)
}

I am so happy!

Gerard



More information about the R-sig-Geo mailing list