0

I would like to merge multiple SpatialPolygonDataFrames into one. For Belgium & the Netherlands this works OK using maptools' spRbind:

td <- tempdir()
bel <- getData("GADM", country="BEL", level=1, path=td)
nld <- getData("GADM", country="NLD", level=1, path=td)
row.names(nld) <- paste("nld", row.names(nld), sep="_")
row.names(bel) <- paste("bel", row.names(bel), sep="_")
library(maptools)
bel_nld <- spRbind(bel, nld)
plot(bel_nld)

However, in my case I would like to merge the province outlines for both Belgium & the Netherlands, ie

td <- tempdir()
bel <- getData("GADM", country="BEL", level=2, path=td)
nld <- getData("GADM", country="NLD", level=1, path=td)
row.names(nld) <- paste("nld", row.names(nld), sep="_")
row.names(bel) <- paste("bel", row.names(bel), sep="_")
library(maptools)
bel_nld <- spRbind(bel, nld)
plot(bel_nld)

but then I get the error message "Error in rbind(deparse.level, ...) : numbers of columns of arguments do not match"

Would anybody know how to resolve this, and obtain the provincial outlines of Belgium & the Netherlands in one SpatialPolygonDataFrame?

1
  • What attributes do you want in your merged dataset? nld only has names up to level 1, but bel has level 1 and level 2 names. If you just want a name for each region, drop all the other columns and create two spdfs with a name column...
    – Spacedman
    Commented Oct 30, 2017 at 12:24

1 Answer 1

1

Drop all the attributes except the unique name. Keep other ones if you want, just make sure you have the same number in each:

> bel = bel[,"NAME_2"]
> nld = nld[,"NAME_1"]

You can't spRbind if the names are different so make them the same:

> names(bel)="NAME"
> names(nld)="NAME"

If you've kept the country code as well in the first step then names(bel)=c("COUNTRY","NAME") might be needed.

Once that's done:

> belnld = spRbind(bel, nld)

If you want to keep all the attributes of each country, then you probably need to construct two spdfs with all the attributes, filling in NA where one column doesn't exist in the other data set.

1
  • Many thanks! That did the trick (after making the province names unique)! Commented Oct 30, 2017 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.