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

Subsetting by index

The subsetting of Spatial___DataFrame objects is built to work like subsetting a data frame. You think about subsetting the data frame, but in practice what is returned is a new Spatial___DataFrame with only the rows of data you want and the corresponding spatial objects.

The simplest kind of subsetting is by index. For example, if x is a data frame you know x[1, ] returns the first row. If x is a Spatial___DataFrame, you get a new Spatial___DataFrame that contains the first row of data and the spatial data that correspond to that row.

The benefit of returning a Spatial___DataFrame is you can use all the same methods as on the object before subsetting.

Let's test it out on the 169th country!

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

Visualizing Geospatial Data in R

ดูคอร์ส

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

  • Create a new variable usa by subsetting the 169th element of countries_spdf.
  • Call summary() on usa. Verify usa is still a SpatialPolygonsDataFrame.
  • Call str() with max.level = 2 on usa. Verify there is only one element of the polygons slot and only one row in the data slot.
  • Call plot() on usa.

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

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

# Subset the 169th object of countries_spdf: usa


# Look at summary() of usa


# Look at str() of usa


# Call plot() on usa
แก้ไขและรันโค้ด