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?
Este exercício faz parte do curso
Visualizing Geospatial Data in R
Instruções do exercício
- Create a logical vector called
is_nzthat tests if thenamecolumn is equal to"New Zealand". - Create a new spatial object called
nzby usingis_nzto subsetcountries_spdf. - Plot
nz.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Create logical vector: is_nz
# Subset countries_spdf using is_nz: nz
# Plot nz