[R-sig-Geo] Recalling shapefile attributes to apply to polygons using a loop

Robert J. Hijmans r.hijmans at gmail.com
Mon Jan 20 21:45:46 CET 2014


Brent, In your questions to the list, please use reproducible
examples, as below.

# get a SpatialPolygonsDataFrame
library(raster)
p <- shapefile(system.file("external/lux.shp", package="raster"))

# add sample size variable
p$samplingintensity <- ceiling(runif(length(p)) * 10)
head(p)

# sample:
allocation <- sapply(1:length(p), function(i) spsample(p[i,],
n=p$samplingintensity[i], type='random'))


Best, Robert

On Mon, Jan 20, 2014 at 9:11 AM, Brent Wilson <brent.wilson at unb.ca> wrote:
> I have a list of polygons from a shapefile that I'm looping through to
> assign random sampling locations to. In the loop, I wish to access the
> original polygon attributes table to apply a sampling intensity that is
> unique to each polygon (from a column under 'samplingintensity'). Is there
> a way to apply this (e.g., n = polygon$samplingintensity) or are the
> polygon attributes stripped away at this point using the slot function?
>
> # read in polygon shapefile
> strataboundaries <- readOGR('strataboundaries.shp',
> layer='strataboundaries')# allocate samples to polygons
> allocation <- sapply(slot(strataboundaries, 'polygons'), function(i)
> spsample(i, *n=3*, type='random'))
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list