I am attempting to create a choropleth by following Geospatial Health Data by Paula Moraga, Chapter 15. The data can be downloaded from the book info page. It is the Chapter 15 data from the DOWNLOAD APPDIR FOLDER link.
The choropleth I am trying to duplicate can be found in section 15.7.3 The code I am using is directly from the text (with appropriate changes for my working directory):
data <- read.csv("data/data.csv")
map <- readOGR("data/fe_2007_39_county/fe_2007_39_county.shp")
datafiltered <- data[which(data$year == 1980), ]
ordercounties <- match(map@data$NAME, datafiltered$county)
map@data <- datafiltered[ordercounties, ]
However, the map@data results in an error:
Error in (function (cl, name, valueClass) :
c("assignment of an object of class “sf” is not valid for @‘data’ in an object of class “SpatialPolygonsDataFrame”; is(value, \"data.frame\") is not TRUE", "assignment of an object of class “data.frame” is not valid for @‘data’ in an object of class “SpatialPolygonsDataFrame”; is(value, \"data.frame\") is not TRUE")
How do I resolve this error?
Using Chris's advice below, I tried a new terminal but that didn't help. Running sf:sf_extSoftVersion()
gave me:
GEOS GDAL proj.4 GDAL_with_GEOS USE_PROJ_H PROJ
"3.9.1" "3.2.1" "7.2.1" "true" "true" "7.2.1"
Problem still unresolved.