I am trying to do Spatial + Non-Spatial object join here. However, I am trying to get the spatial coordinates of the matching ids. I tried to use sp::merge, but that is doing the other way around.

My dataframe has ids, I am trying to get the spatial boundaries of those ids alone from spatial polygons data frame.

df looks like,


spatialpolygondf will also have a column called id in its attributes. How can I join them together to get the boundaries ?

  • 1
    Can you make a reproducible example using sample data?
    – Spacedman
    Commented Apr 30, 2018 at 6:53
  • give a try to library(sf), then the standard dplyr verbs may be used; if you use library(sp) you should fortify your spatialobject
    – Elio Diaz
    Commented Apr 30, 2018 at 15:00

1 Answer 1


From what I'm gathering, it looks a combination of the sf and dplyr packages might work.

#load files:
spatial <- st_read("C:/filepath/filename") 
nonspatial<- read.csv("C:/filepath/filename")

#use inner_join:
df1 <- inner_join(spatial, nonspatial, by = "ID")

This should return all the rows from your spatial object, where there are matching values in the non-spatial object and all the columns from both.

You can use st_boundary() with mutate(), to create a new boundary column for the IDs. Then, use select function() in dplyr to create a smaller dataframe with your ID and spatial boundaries columns.

#data for analysis:
  mutate(df1, boundaries_ col = st_boundary(ID))
  select(ID, boundaries_col)

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.