3

I am attempting to merge a dataframe containing data on hospital visits ("Hospital_Visits_df") with a shapefile/spatial*dataframe containing ZIP/ZCTA polygons and coordinates ("shp", downloaded from the Census Bureau).

Both dataframes contain a matching column, ZCTA and GEOID10, respectively, though shp contains polygons for the entirety of the USA, which I will pare down to the relevant states later.

I have attempted using both merge() and left_join(), but both results in their own errors.

shp_hospital_zip1 <- merge(shp, Hospital_Visits_df, by.x = "GEOID10", by.y = "ZCTA")

Error in .local(x, y, ...) : non-unique matches detected

But there are no duplicates in either column.

shp_hospital_zip2 <- shp@data %>% 
   left_join(Hospital_Visits_df, by = c("GEOID10" = "ZCTA"))

which appeared to work, except when I went to summary(shp_hospital_zip2) it no longer specifies it as a spatial data frame.

I am looking to merge my hospital visit data with the spatial data so that I can plot it with leaflet, but I am getting tripped up at the first step.

I appreciate any help you can give me. Thank you very much !!

demarsylvain
  • 2,103
  • 2
  • 14
  • 33

2 Answers2

5

I think you need to use left_join() on your sf-object directly, not on the @data part of your sf-object.

library(sf)

class(shp)
[1] "sf"         "data.frame"

class(Hospital_Visits_df)
[1] "data.frame"

result <- shp %>% 
  left_join(Hospital_Visits_df)
demarsylvain
  • 2,103
  • 2
  • 14
  • 33
2
library(rgdal)
mydf   <- read.csv("myCsv.csv")
myspdf <- readOGR("myShapefile.shp")

mynewspdf <- merge(myspdf, mydf, duplicateGeoms = T)

Reference

akash87
  • 3,876
  • 3
  • 14
  • 30