Skip to content
Prev 5106 / 29559 Next

stratified random sampling

Hi Gerard,

following Dylan's hint, the following example should be easily reproducable:

library(maptools)
nc1 <- readShapePoly(system.file("shapes/sids.shp",
package="maptools")[1], proj4string=CRS("+proj=longlat +datum=NAD27"))
x100 = do.call(rbind, sapply(slot(nc1, "polygons"), spsample, n=1,
type="random"))
spplot(nc1[1], sp.layout=list(sp.points, x100,col='black'))

the do.call and sapply are of course a bit scary; you could replace it
with with a for loop:

x = slot(nc1, "polygons")
for (i in 1:length(x)) {
        pt = spsample(x[[i]], n = 1, type = "random")
        if (i == 1)
                x100 = pt
        else
                x100 = rbind(x100, pt)
}

to get something perfectly equivalent but less efficient (and more error
prone).

Note that x100 does not have the CRS anymore (design flaw?); you'd need
to do a

proj4string(x100) = proj4string(nc1)
--
Edzer
Dylan Beaudette wrote: