เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

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.

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Visualizing Geospatial Data in R

ดูคอร์ส

คำแนะนำการฝึกหัด

  • 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.

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# 169th element of countries_spdf@polygons: one


# Print one


# Call summary() on one


# Call str() on one with max.level = 2
แก้ไขและรันโค้ด