So I have two shapefile layers of U.S. Census tracts with data I need to merge, but unfortunately one of the files uses a peculiar ID system that does not match up with the other file. I would like to merge these data into one layer, but I'm struggling to figure out how to do this in R (in QGIS, I can do this with the "Data Management Tools > Join Attributes by Location" function. Below is some code used to create two polygon layers, now I'd like to determine how to combine these two layers into one which retains all underlying data from both layers.
library(sp)
# Create FIRST polygon layer
square <- rbind(c(255842.4, 4111578, 255862.4, 4111578, 255862.4, 4111558,
255842.4, 4111558, 255842.4, 4111578, 255842.4, 4111578),
c(257397.0, 4111309, 257417.0, 4111309, 257417.0, 4111289,
257397.0, 4111289, 257397.0, 4111309, 257397.0, 4111309))
ID <- c("C1", "C2")
people <- c(5000, 3000)
polys <- SpatialPolygons(list(
Polygons(list(Polygon(matrix(square[1, ], ncol=2, byrow=TRUE))), ID[1]),
Polygons(list(Polygon(matrix(square[2, ], ncol=2, byrow=TRUE))), ID[2])
))
# Create a dataframe and display default rownames
( p.df <- data.frame( ID=1:length(polys)) )
rownames(p.df)
# Try to coerce to SpatialPolygonsDataFrame (will throw error)
polys <- SpatialPolygonsDataFrame(polys, p.df)
# Extract polygon ID's
( pid <- sapply(slot(polys, "polygons"), function(x) slot(x, "ID")) )
# Create dataframe with correct rownames
( p.df <- data.frame( ID=1:length(polys), row.names = pid) )
# Try coersion again and check class
p <- SpatialPolygonsDataFrame(polys, p.df)
class(p)
# Now we can add a column
p@data$people <- c(7500,3000)
##################### NOW, creating the SECOND polygon layer
square2 <- rbind(c(255842.4, 4111578, 255862.4, 4111578, 255862.4, 4111558,
255842.4, 4111558, 255842.4, 4111578, 255842.4, 4111578),
c(257397.0, 4111309, 257417.0, 4111309, 257417.0, 4111289,
257397.0, 4111289, 257397.0, 4111309, 257397.0, 4111309))
ID2 <- c("30067893", "30794385")
polys2 <- SpatialPolygons(list(
Polygons(list(Polygon(matrix(square[1, ], ncol=2, byrow=TRUE))), ID2[1]),
Polygons(list(Polygon(matrix(square[2, ], ncol=2, byrow=TRUE))), ID2[2])
))
# Create a dataframe and display default rownames
( p2.df <- data.frame( ID=1:length(polys2)) )
rownames(p2.df)
# Try to coerce to SpatialPolygonsDataFrame (will throw error)
polys2 <- SpatialPolygonsDataFrame(polys2, p2.df)
# Extract polygon ID's
( pid2 <- sapply(slot(polys2, "polygons"), function(x) slot(x, "ID")) )
# Create dataframe with correct rownames
( p2.df <- data.frame( ID=1:length(polys2), row.names = pid2) )
# Try coersion again and check class
p2 <- SpatialPolygonsDataFrame(polys2, p2.df)
class(p2)
# Now we can add a column
p2@data$homes <- c(500,250)
Now, I'd like to merge the data together to have one SpatialPolygonsDataFrame which has both "homes" and "people" as columns. This post is asking something similar, however, imagine that the ID column does not match up between the two layers. Anyone have a good notion for how I could go about doing that?