title: Allocating random samples to polygons
body: <p>I am trying to script a random allocation routine. The sampling design has a region divided into numerous polygons or strata. A set, but different number of samples are to be allocated randomly to each strata (a minimum of 2 samples but as many as 7 in some strata). As such, I have a shapefile of the strata and in its' attribute table, the strata names and number of samples desired in each.</p>
  
  <ul>
  <li>STRATA; SAMPLES</li>
  <li>440; 4</li>
  <li>441; 2</li>
  <li>5Z3; 4</li>
  <li>5Z1; 7</li>
  <li>560; 2</li>
  </ul>
  
  <p>I found some good documentation on these types of sampling designs (<a href="http://casoilresource.lawr.ucdavis.edu/drupal/book/export/html/519" rel="nofollow">http://casoilresource.lawr.ucdavis.edu/drupal/book/export/html/519</a>) although I'm having some issues implementing the routine for my own needs. Following this link, I have been working with both rgdal and maptools packages. My working script is, as follows: </p>
  
  <pre><code># read in strata boundary shapefile 
  strataboundaries &lt;- readOGR('strataboundaries.shp', layer='strataboundaries')
  #sample allocation to strata
  allocation &lt;- sapply(slot(strataboundaries, 'polygons'), function(i) spsample(i, n=4, type='random'))
  allocation.merge &lt;- do.call('rbind', allocation)
  stratumID &lt;- sapply(slot(strataboundaries, 'polygons'), function(i) slot(i, 'ID'))
  sample &lt;- sapply(allocation, function(i) nrow(i@coords))
  sampleID &lt;- rep(stratumID, sample)
  allocation.final &lt;- SpatialPointsDataFrame(allocation.merge, 
  data=data.frame(poly_id=sampleID))
  plot(strataboundaries, col="lightcyan", bborder="black", axes=TRUE, bg="lightsteelblue1")
  points(allocation.final, col="red", pch=3, cex=0.8)
  #write out shapefile containing sampling locations
  allocation.final@proj4string &lt;- strataboundaries@proj4string
  writeOGR(allocation.final, ".", "allocation", driver='ESRI Shapefile')
  </code></pre>
  
  <p>However, the sampling intensity per stratum is not static (where I have n=4). I need this to reflect the column in the attributes table, which indicates the respective number of samples required for a given strata. I would also like to assign the strata names back to the sampling locations that have been allocated. </p>
  
  <p>Ideally, the routine would iterate though each polygon and assign n number of samples randomly within (as indicated in the attribute table) and be written as a shapefile. Any direction would be greatly appreciated. I'm relatively new to the program so I apologize if I've missed something obvious.</p>
  
  <pre><code>[1] "SpatialPolygonsDataFrame"
  attr(,"package")
  [1] "sp"
  
  Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
    ..@ data       :'data.frame': 66 obs. of  3 variables:
    .. ..$ OBJECTID_1: int [1:66] 1 2 3 4 5 6 7 8 9 10 ...
    .. ..$ Stratum1  : Factor w/ 66 levels "440","441","442",..: 65 64 63 62 61 60 12 11 7 49 ...
    .. ..$ Primary   : int [1:66] 2 2 4 5 2 7 2 2 2 2 ...
    ..@ polygons   :List of 66
    .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
    .. .. .. ..@ Polygons :List of 1
    .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
    .. .. .. .. .. .. ..@ labpt  : num [1:2] -68.3 40.4
    .. .. .. .. .. .. ..@ area   : num 0.769
    .. .. .. .. .. .. ..@ hole   : logi FALSE
    .. .. .. .. .. .. ..@ ringDir: int 1
    .. .. .. .. .. .. ..@ coords : num [1:654, 1:2] -66.3 -66.3 -66.4 -66.4 -66.4 ...
    .. .. .. ..@ plotOrder: int 1
    .. .. .. ..@ labpt    : num [1:2] -68.3 40.4
    .. .. .. ..@ ID       : chr "0"
    .. .. .. ..@ area     : num 0.769
  </code></pre>