简体   繁体   English

如何根据 R 中的属性删除 SpatialPolygonsDataFrame 的特定功能?

[英]How to remove specific features of SpatialPolygonsDataFrame based on attributes in R?

Since it is not appreciated to ask a question under an already existing question which has been answered, here my question linking to this question URL: Simple way to subset SpatialPolygonsDataFrame (ie delete polygons) by attribute in R where you can also acquire the dataset.由于在已经回答的现有问题下提出问题是不受欢迎的,这里我的问题链接到这个问题 URL: Simple way to subset SpatialPolygonsDataFrame (ie delete polygons) by attribute in R在那里你也可以获取数据集。

Does anyone have an idea how to remove multiple features from the SpatialPolygonsDataFrame based on a vector of values?有没有人知道如何根据值向量从 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

I would like to remove the features which have the NAMES 'Finland', 'Norway', 'Sweden', 'Denmark' (no offense, I just do not have data for Scandinavia ;) ).我想删除名称为“芬兰”、“挪威”、“瑞典”、“丹麦”的特征(无意冒犯,我只是没有斯堪的纳维亚的数据;))。

Following approaches failed:以下方法失败:

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

I would guess that there is an approach using base::which() and %in% , but I do not have any idea how to go on.我猜想有一种使用base::which()%in% ,但我不知道如何继续。 The answer of @Jeffrey Evans in this post see URL: https://gis.stackexchange.com/questions/57844/subset-a-spatialpolygonsdataframe-by-id-in-r could provide a useful hint... @Jeffrey Evans 在这篇文章中的回答见 URL: https ://gis.stackexchange.com/questions/57844/subset-a-spatialpolygonsdataframe-by-id-in-r 可以提供一个有用的提示......

You may subset the SpatialPolygonsDataFrame by row ids:您可以按行 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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在R中将具有基于位置的数据的列添加到SpatialPolygonsDataFrame中? - How to add a column with location-based data to a SpatialPolygonsDataFrame in R? 如何重命名 R 中 SpatialPolygonsDataFrame 的特定列中的条目? - How do I rename entries in a specific column of a SpatialPolygonsDataFrame in R? 带R的SpatialPolygonsDataFrame中重叠多边形的属性值求和 - Summing values of attributes of overlapping polygons in SpatialPolygonsDataFrame with R R-栅格化SpatialPolygonsDataFrame-删除变量/属性 - R - Rasterize a SpatialPolygonsDataFrame - variables/attributes dropped 如何删除R中不需要的功能? - How to remove unwanted features in R? 如何根据特定条件删除 R 中的行 - How to remove rows in R based on specific criteria 如何在 R 中使用字符串搜索从大型 SpatialPolygonsDataFrame 中删除多边形的子集? - How do I remove a subset of polygons from a Large SpatialPolygonsDataFrame using a string search, in R? R Shiny:如何在输出基于UI selectInput()的合并SpatialPolygonsDataFrame之前过滤数据帧? - R Shiny: how to filter a dataframe before outputting a merged SpatialPolygonsDataFrame based on UI selectInput()? 在R中,我如何加入和分配SpatialPolygonsDataFrame? - In R, how do I join and subset SpatialPolygonsDataFrame? 栅格化多个SpatialPolygonsDataframe属性 - Rasterize multiple SpatialPolygonsDataframe attributes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM