Get startedGet started for free

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.

This exercise is part of the course

Visualizing Geospatial Data in R

View Course

Exercise instructions

  • Create a new variable called one that contains the 169th element of the list in the polygons slot of countries_spdf. Use double bracket subsetting (i.e. [[...]] to extract this element.
  • Print one.
  • Call summary() on one. What slots does this object have?
  • Call str() on one with max.level = 2.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 169th element of countries_spdf@polygons: one


# Print one


# Call summary() on one


# Call str() on one with max.level = 2
Edit and Run Code