How do I merge polygon features in a shapefile with many polygons?
rbind and union just combine the rows of the shapefile features, they don't actually merge the polygons themselves.
Desired result in example below:
How do I get the below shapefile with duplicated ID_2 to merge to a single polygon in sptemp?
Example below of GADM level 2 of Ethiopia has first two rows of the shapefile ID_2 column duplicated (value=1). I'd like sptemp with 79 features combining the first two rows that are the ones with duplicated ID_2. A plot of sptemp[1,] would bring up where current sptemp[1,] and sptemp2[2,] without the boundaries between the duplicated, i.e. the polygons are merged too.
Example Code:
Download, unzip, and load into R GADM file for Ethiopia level 2 (899kb to working directory):
library(curl)
library(rgdal)
curl_download("http://biogeo.ucdavis.edu/data/gadm2.8/shp/ETH_adm_shp.zip",
destfile=paste0("gadmETH.zip"),
quiet=FALSE)
unzip(paste0("gadmETH.zip"), exdir="gadmETH", overwrite=FALSE)
###Load shapefile
sptemp <- readOGR(dsn="gadmETH", layer="ETH_adm2")
The ID_2 column for the first two polygons is duplicated
###You'll see in the first two rows ID_2 is duplicated
df.sptemp <- as.data.frame(sptemp)
View(sptemp)
###I can't just delete one because they are separate polygons
plot(sptemp[1,], col="blue")
plot(sptemp[2,], add=TRUE, col="red" )