Walking the hierarchy
Let's practice accessing slots by exploring the way polygons are stored inside SpatialDataFrame objects. Remember there are two ways to access slots in an S4 object:
x@slot_name # or...
slot(x, "slot_name")
So, to take a look at the polygons slot of countries_spdf you simply do countries_spdf@polygons. You can try it, but you'll get a long and not very informative output. Let's look at the high level structure instead.
Try running the following code in the console:
str(countries_spdf@polygons, max.level = 2)
Still a pretty long output, but scroll back to the top and take a look. What kind of object is this? It's just a list, but inside its elements are another kind of sp class: Polygons. There are 177 list elements. Any guesses what they might represent?
Let's dig into one of these elements.
Este exercício faz parte do curso
Visualizing Geospatial Data in R
Instruções do exercício
- Create a new variable called
onethat contains the 169th element of the list in thepolygonsslot ofcountries_spdf. Use double bracket subsetting (i.e.[[...]]to extract this element. - Print
one. - Call
summary()onone. What slots does this object have? - Call
str()ononewithmax.level = 2.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# 169th element of countries_spdf@polygons: one
# Print one
# Call summary() on one
# Call str() on one with max.level = 2