如何根据 R 中的属性删除 SpatialPolygonsDataFrame 的特定功能?
How to remove specific features of SpatialPolygonsDataFrame based on attributes in R?
由于在已经回答的现有问题下提问是不受欢迎的,这里我的问题链接到这个问题URL:Simple way to subset SpatialPolygonsDataFrame (i.e. delete polygons) by attribute in R 在那里你也可以获取数据集。
有谁知道如何根据值向量从 SpatialPolygonsDataFrame 中删除多个特征?
head(world.map@data)
# FIPS ISO2 ISO3 UN NAME AREA POP2005 REGION SUBREGION LON LAT
# 0 AC AG ATG 28 Antigua and Barbuda 44 83039 19 29 -61.783 17.078
# 1 AG DZ DZA 12 Algeria 238174 32854159 2 15 2.632 28.163
# 2 AJ AZ AZE 31 Azerbaijan 8260 8352021 142 145 47.395 40.430
# 3 AL AL ALB 8 Albania 2740 3153731 150 39 20.068 41.143
# 4 AM AM ARM 51 Armenia 2820 3017661 142 145 44.563 40.534
# 5 AO AO AGO 24 Angola 124670 16095214 2 17 17.544 -12.296
我想删除名称为'Finland'、'Norway'、'Sweden'、'Denmark'(没有冒犯,我只是没有斯堪的纳维亚半岛的数据 ;))。
以下方法失败:
world.map <- world.map[world.map@data$NAME != c('Finland', 'Norway', 'Sweden', 'Denmark'), ]
# does not work at all
world.map <- dplyr::filter(world.map@data, NAMEN %in% c('Finland', 'Norway', 'Sweden', 'Denmark'))
# results in a dataframe, spatial information is lost
我猜想有一个使用 base::which()
和 %in%
的 方法,但我不知道如何继续。 @Jeffrey Evans 在 post 中的回答见 URL:https://gis.stackexchange.com/questions/57844/subset-a-spatialpolygonsdataframe-by-id-in-r 可以提供有用的提示...
您可以按行 ID 对 SpatialPolygonsDataFrame
进行子集化:
# row numbers of countries, exluding scandinavia
ids <- which(
!(world.map$NAME %in% c('Finland', 'Norway', 'Sweden', 'Denmark'))
)
# subset
not_scandinavia <- world.map[ids, ]
# plot
plot(not_scandinavia)
由于在已经回答的现有问题下提问是不受欢迎的,这里我的问题链接到这个问题URL:Simple way to subset SpatialPolygonsDataFrame (i.e. delete polygons) by attribute in R 在那里你也可以获取数据集。
有谁知道如何根据值向量从 SpatialPolygonsDataFrame 中删除多个特征?
head(world.map@data)
# FIPS ISO2 ISO3 UN NAME AREA POP2005 REGION SUBREGION LON LAT
# 0 AC AG ATG 28 Antigua and Barbuda 44 83039 19 29 -61.783 17.078
# 1 AG DZ DZA 12 Algeria 238174 32854159 2 15 2.632 28.163
# 2 AJ AZ AZE 31 Azerbaijan 8260 8352021 142 145 47.395 40.430
# 3 AL AL ALB 8 Albania 2740 3153731 150 39 20.068 41.143
# 4 AM AM ARM 51 Armenia 2820 3017661 142 145 44.563 40.534
# 5 AO AO AGO 24 Angola 124670 16095214 2 17 17.544 -12.296
我想删除名称为'Finland'、'Norway'、'Sweden'、'Denmark'(没有冒犯,我只是没有斯堪的纳维亚半岛的数据 ;))。
以下方法失败:
world.map <- world.map[world.map@data$NAME != c('Finland', 'Norway', 'Sweden', 'Denmark'), ]
# does not work at all
world.map <- dplyr::filter(world.map@data, NAMEN %in% c('Finland', 'Norway', 'Sweden', 'Denmark'))
# results in a dataframe, spatial information is lost
我猜想有一个使用 base::which()
和 %in%
的 方法,但我不知道如何继续。 @Jeffrey Evans 在 post 中的回答见 URL:https://gis.stackexchange.com/questions/57844/subset-a-spatialpolygonsdataframe-by-id-in-r 可以提供有用的提示...
您可以按行 ID 对 SpatialPolygonsDataFrame
进行子集化:
# row numbers of countries, exluding scandinavia
ids <- which(
!(world.map$NAME %in% c('Finland', 'Norway', 'Sweden', 'Denmark'))
)
# subset
not_scandinavia <- world.map[ids, ]
# plot
plot(not_scandinavia)