I have a bunch of polygons representing land cover classes, however I don't need this much detail. I would like to merge small polygons (ie < 150m2) with it's largest neighbour. This would be similar to "Eliminate" in ArcGIS Pro or "eliminate selected polygons" in QGIS. This process will be repeated so I'd like to script it in R and save some time.
The only way I could think to do it was to add a column indicating which polygons were under 150m2, then somehow merging them to nearest neighbor.
#Packages
library(terra)
library(sf)
library(dplyr)
#Adding polygons from Terra and converting to sf
v <- vect(system.file("ex/lux.shp", package="terra"))
v <- st_as_sf(v[c(1:12)])
#Adding a column indicating which polygons are under 150m2
mutate(v, area_thresh = case_when(AREA < 150 ~ 1,
AREA > 150 ~ 0))
This is an interesting problem, as you need to iterate over rows of changing shapefile (your number of rows will be decreasing as you merge smaller objects).
For a possible approach consider this piece of code that builds upon the well known & much loved NC shapefile that ships with
{sf}
.Note that I am using the county_id as a unique identifier; a merged polygon will keep ID of the larger county.