2

I have two SpatialPolygonsDataFrames. One is the LSOA shapefile of Great Britain (i.e., Great Britain divided into ~41,000 regions with approximately equal population densities), the other is a bunch of regions in Great Britain (coal fields). I want to add the coal field info to the LSOA object, in order to have a variable added to the LSOA object that tells me whether an LSOA region falls into a coal field or not.

I got the shapefiles from here:

This is how I attempted to do the merge:

# read in LSOA regions
LSOA <- readOGR(dsn = "Infuse", layer = "infuse_lsoa_lyr_2011")
# transform map to OSGB projection
bng = '+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs'
LSOA.bng <- spTransform(LSOA,bng)

# read in coalmining regions:
coalmine <- readOGR(dsn = "coalmining_shapefiles", layer = "Coalfield_polygon")
# transform map to OSGB projection
coalmine.bng <- spTransform(coalmine,bng)

# merge data
coalmines.LSOA.over <- over(LSOA.bng, coalmine.bng)
LSOA.coalmines <- spCbind(LSOA.bng,coalmines.LSOA.over)

This is the error message I get after the spCbind command:

Error in spCbind(LSOA.bng, coalmines.LSOA.over) : row names not identical

What did I do wrong?

5
  • 1
    as over's result comes out ordered you don't need to merge/join; just add it as another column to your LSOA.bng
    – Elio Diaz
    Commented Apr 11, 2018 at 17:17
  • How do I do that?
    – Abdel
    Commented Apr 11, 2018 at 17:26
  • 1
    lsoa.bng$var <- coalmines.lsoa.over$your_target_variable
    – Elio Diaz
    Commented Apr 11, 2018 at 17:32
  • Thank you very much! If you post it as an answer, I can accept it as the right answer and give you your credits :)
    – Abdel
    Commented Apr 11, 2018 at 17:53
  • Ok, I appreciate that
    – Elio Diaz
    Commented Apr 11, 2018 at 18:09

1 Answer 1

3

As the output of sp::over is ordered to the input, you don't need to join/merge the two objects; you only need to add the targeted (or desired) columns from the output of over to your data frame.

Using base r, you define a new variable of your data frame with the $ operator, then you assign the output of over to it:

lsoa.bng$var <- coalmines.lsoa.over$your_target_variable

if it doesn't match a name already in your df, you may also cbind it:

lsoa.bng <- cbind(lsoa.bng, coalmines.lsoa.over$your_target_variable)

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.