我认为这是你在找什么。如果要合并连续区域的连续成员,这会有所不同,但是您只需从多边形中移除第一个元素(整个状态),然后在其余区域上运行gUnaryUnion,然后重新添加邻接的成员到gUnaryUnion -ized状态的副本。
oregon = oregon.tract
names(attributes(oregon.tract))
#[1] "bbox" "proj4string" "polygons" "plotOrder" "data"
#[6] "class"
selected_tract_indices = which([email protected]$tract == 9501)
oregon <- gUnaryUnion(oregon.tract, id = [email protected]$state)
d = oregon
npolygons = 1
for (selected_tract_index in selected_tract_indices){
[email protected][[npolygons+1]] = [email protected][[selected_tract_index]]
npolygons = npolygons + 1
[email protected]=c([email protected],as.integer(npolygons))
}
plot(d)
该操作的输出是一个SpatialPolygon。如果您想将其转换回SpatialPolygonDataDrame,here是一种简单的方法:
# Extract polygon ID's
(did <- sapply(slot(d, "polygons"), function(x) slot(x, "ID")))
# Create dataframe with correct rownames
(d.df <- data.frame(ID=1:length(d), row.names = did))
# Try coersion again and check class
d <- SpatialPolygonsDataFrame(d, d.df)
class(d)