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 !!