Open malmufre opened 3 years ago
The merge() function is able to combine an sp object with a date.frame object.
It cannot user merge() with an sf object. You could have to cast the sf object as an sp object first.
What is the class of this object here?
msp_dorling <- tracts( state="48", county="201", cb=TRUE, year=2015 )
class( msp_dorling )
I suspect it's an sf object. It's also a regular polygon shapefile, not a dorling shapefile, but that is just a variable name issue that should cause an error.
If you go back through the labs there are examples of conversions from sf to sp, or sp to sf:
# convert sf map object to an sp version
msp.sp <- as_Spatial( msp )
The class of the object
msp_dorling <- tracts( state="48", county="201", cb=TRUE, year=2015 )
class( msp_dorling )
is an sf object.
I suspect that I am doing something wrong in the steps of creating my Dorling cartogram . Up to this step my msp.sp is an sp object.
msp.sp <- as_Spatial( msp )
class( msp.sp )
I went through the steps we did in lab 4 , however when I saw the steps in the link provided in the course schedule https://ds4ps.org/cpp-529-fall-2020/LABS/save-dorling-cartogram.html I saw that there was the mhhi variable added so I am guessing this is where I went wrong.
Ok, I think I see the issue here. If you are trying to preview data you need to this syntax for the sp objects (you reference the embedded data frame using the @data
slot):
head( msp.sp@data )
But you can use the basic version for sf objects since they embed the polygons inside the dataframe itself:
head( msp.sf )
So it wasn't an issue with your merge step, it was just the preview step.
Hello,
I am going through the steps to create a Dorling cartogram for the city that I chose, and I have reached this step.
msp_dorling <- merge( msp_dorling, d, by.x="GEOID", by.y="tractid", all.x=T ) class(msp_dorling)
and up to this step where I merge my Dorling cartogram with object d (which contains the census data) , the class of the msp_dorling is an sp object.However when I try to add these variables to my Dorling cartogram the
msp_dorling
changes to an sf object.Then when I try this
I get this error . Error in head(msp_dorling@data) : trying to get slot "data" from an object (class "sf") that is not an S4 object.
I have loaded the libraries
sf
sp
andrgdal
but It still did not seem to work. Where am I going wrong?Thanks