I am not a pro with R nor spatial analysis. I am looking for a way to combine the polygons inside a spatial polygons data frame based on a field in the @data slot: the equivalent of dplyr's "group_by" for spdf's.
I'm not sure if merge, join, or combine are the right words but I hope it is clear what I'm looking for.
library(sp)
#coordinates:
xy1 = cbind(c(1,2,2,1),c(1,1,2,2))
xy2 = cbind(c(2,3,3,2),c(1,1,2,2))
xy3 = cbind(c(1,2,2,1),c(2,2,3,3))
xy4 = cbind(c(2,3,3,2),c(2,2,3,3))
#polygons:
p1 = Polygon(xy1)
ps1 = Polygons(list(p1),ID = "a")
p2 = Polygon(xy2)
ps2 = Polygons(list(p2),ID = "b")
p3 = Polygon(xy3)
ps3 = Polygons(list(p3),ID = "c")
p4 = Polygon(xy4)
ps4 = Polygons(list(p4),ID = "d")
#spatial polygons:
sps_m = SpatialPolygons(list(ps1,ps2,ps3,ps4))
#dataframe:
data_m = data.frame(dt = c("Group A","Group B","Group A","Group C"),row.names = c("a","b","c","d"))
#spatial polygons dataframe:
spdf_m = SpatialPolygonsDataFrame(sps_m,data_m)
#plot spdf:
plot(spdf_m)