[R-sig-Geo] stratified random sampling

Edzer Pebesma edzer.pebesma at uni-muenster.de
Mon Feb 23 21:30:11 CET 2009


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:
> On Monday 23 February 2009, Heuvelink, Gerard wrote:
>   
>> Dear list,
>>
>> I use readShapePoly to import a shape file. The file has multiple polygons
>> (actually, a 'polygon' may consist of multiple polygons that have the same
>> ID). I want to sample one location at random from each of the polygons. I
>> tried to do this with spsample but did not manage (the option "stratified"
>> does not do what I want). I also looked at dotsInPolys but also without
>> success.
>>
>> I think it should be possible with spsample, possibly after first creating
>> a large number of SpatialPolygons objects, one for each polygon, and next
>> sampling one location at random from each of these. Unfortunately I am not
>> very experienced and also failed to achieve what I want in this way.
>>
>> Can you help me?
>>
>> Thanks, Gerard Heuvelink
>>
>>     
>
> Does this help:
> http://casoilresource.lawr.ucdavis.edu/drupal/node/644
>
> Cheers,
>
> Dylan
>
>
>   

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de/
http://www.springer.com/978-0-387-78170-9 e.pebesma at wwu.de



More information about the R-sig-Geo mailing list