R 将随机样本分配给多边形
我正在尝试编写一个随机分配例程。采样设计将一个区域划分为许多多边形或地层。每个地层随机分配一组不同数量的样本(至少2个样本,但在某些地层中多达7个)。因此,我有一个地层的形状文件,在它的属性表中,有地层名称和每个地层中所需的样本数量R 将随机样本分配给多边形,r,spatial,shapefile,random-sample,R,Spatial,Shapefile,Random Sample,我正在尝试编写一个随机分配例程。采样设计将一个区域划分为许多多边形或地层。每个地层随机分配一组不同数量的样本(至少2个样本,但在某些地层中多达7个)。因此,我有一个地层的形状文件,在它的属性表中,有地层名称和每个地层中所需的样本数量 地层;样品 440;四, 441人;二, 5Z3;四, 5Z1;七, 560;二, 我在这些类型的抽样设计()上找到了一些很好的文档,尽管我在为自己的需要实现例程时遇到了一些问题。通过这个链接,我一直在使用rgdal和maptools软件包。我的工作脚本如下:
- 地层;样品
- 440;四,
- 441人;二,
- 5Z3;四,
- 5Z1;七,
- 560;二,
# read in strata boundary shapefile
strataboundaries <- readOGR('strataboundaries.shp', layer='strataboundaries')
#sample allocation to strata
allocation <- sapply(slot(strataboundaries, 'polygons'), function(i) spsample(i, n=4, type='random'))
allocation.merge <- do.call('rbind', allocation)
stratumID <- sapply(slot(strataboundaries, 'polygons'), function(i) slot(i, 'ID'))
sample <- sapply(allocation, function(i) nrow(i@coords))
sampleID <- rep(stratumID, sample)
allocation.final <- 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 <- strataboundaries@proj4string
writeOGR(allocation.final, ".", "allocation", driver='ESRI Shapefile')
不幸的是,我无法帮助您在不打开shapefile的情况下执行适当的采样。但让我给你一些建议,也许会有帮助:
[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