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

Subsetting based on data attributes

Subsetting based on data attributes is a combination of creating a logical from the columns of your data frame and subsetting the Spatial___DataFrame object. This is similar to how you subset an ordinary data frame.

Create a logical from a column, let's say countries in Asia:

in_asia <- countries_spdf$region == "Asia"
in_asia

Then, use the logical to select rows of the Spatial___DataFrame object:

countries_spdf[in_asia, ]

Can you subset out New Zealand and plot it?

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

Visualizing Geospatial Data in R

ดูคอร์ส

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

  • Create a logical vector called is_nz that tests if the name column is equal to "New Zealand".
  • Create a new spatial object called nz by using is_nz to subset countries_spdf.
  • Plot nz.

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

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

# Create logical vector: is_nz


# Subset countries_spdf using is_nz: nz


# Plot nz
แก้ไขและรันโค้ด