2

I am trying to merge a .csv file with a .shp file. The shapefile has more information that I need so I also want to limit it by only adding information for the counties in my csv file. I tried my code based on a response to a similar question posted here but received an error. Once I merge them, I would like to make it a shapefile. Here is my code:

library(raster)

county_info <- read.csv("/path/county_info.csv")
county_shape <- readShapePoly("/path/county_shape.shp")
data <- merge(county_info, county_shape, by="GEOID", all.x=TRUE)
data <- shapefile(data, "/path")

The error I receive is:

Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘shapefile’ for signature ‘"data.frame"’
1
  • Without giving an answer to your problem, I advise you to switch to the sf package for vector handling. Much more fun
    – Bernd V.
    Dec 5, 2019 at 17:40

2 Answers 2

3

As aldo_tapia suggests, sf is a great improvement upon the foundation of sp. I would comment if my reputation permitted it.

As for learning sf, the entire Geocomputation with R textbook is available online at https://geocompr.robinlovelace.net/. Everything makes a lot of sense from a traditional R dataframe perspective, and it is dplyr compliant.

That being said, if you are deadset on using sp, you could always access the associated data with county_shape@data, and then merge with your csv. It's ugly and error-prone, but I've definitely done it in the past.

library(raster)

county_info <- read.csv("/path/county_info.csv")
county_shape <- readShapePoly("/path/county_shape.shp")


county_shape@data <- merge(county_info, county_shape@data, by="GEOID", all.x=TRUE)

That being said, learn sf instead of you have the time. Someone else can probably explain in detail why the above is a bad idea!

1

Laura, as Bernd V. suggested, you can use sf for vector management. In this case, use it with dplyr for merging different objects:

library(sf)
library(dplyr)

county_info <- read.csv("/path/county_info.csv")
county_shape <- read_sf("/path/county_shape.shp")
county_info %>% full_join(y = county_shape, by = 'GEOID') %>%
                st_as_sf() %>%
                write_sf("/path/output.shp")

There are different merging methods, like inner_join, left_join, full_join, and so on. Check dplyr package documentation for more info.

2
  • Thank you for your response. I tried your suggestion but received about another error: Error in UseMethod("tbl_vars") : no applicable method for 'tbl_vars' applied to an object of class "c('SpatialPolygonsDataFrame', 'SpatialPolygons', 'Spatial', 'SpatialVector')" Do you know why this is?
    – Laura
    Dec 5, 2019 at 19:41
  • That raised up because you're using a sp object, not a sf object. You can convert it with st_as_sf or load it with read_sf function
    – aldo_tapia
    Dec 5, 2019 at 19:47

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.