首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在使用WDI API之后,如何通过字符串轻松地删除聚集的国家行?

在使用WDI API之后,如何通过字符串轻松地删除聚集的国家行?
EN

Stack Overflow用户
提问于 2020-08-20 06:53:08
回答 1查看 83关注 0票数 1

好吧,我有这个:

代码语言:javascript
复制
library(WDI)
wdi <- WDI(indicator=c("SI.POV.NAHC", "SI.POV.GINI", "SL.UEM.TOTL.ZS",
                       "SP.POP.TOTL"), start=1991, end=2018)
wdi <- as_tibble(wdi)
wdi <- wdi %>% select(-iso2c)
wdi <- wdi %>% rename(`Poverty Headcount`=SI.POV.NAHC, 
                      `Gini Index`=SI.POV.GINI,
                      `Total Unemployment`= SL.UEM.TOTL.ZS, 
                      `Total Population`=SP.POP.TOTL)
##Remove rows command goes here
kable(head(wdi))

如您所见,第一列是country,行是每年的国家观察。我想从这个数据集中删除所有“组”(阿拉伯世界,世界,南亚,.)我只想要国家。

这是世界银行的数据。

我使用的非基本包是:

代码语言:javascript
复制
library(tidyverse)
library(WDI)
library(psych)
library(pastecs)
library(xlsx)
library(stargazer)
library(xtable)
library(markdown)
library(knitr)
library(haven)
library(readr)
library(ggplot2)
library(dplyr)
library(sf)
library(tmap)
library(spData)
library(RColorBrewer)
library(stringr)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-20 07:13:49

您可以检查"iso2c"列的有效性。一个简单的方法是使用countrycode(<values>, <from>, <to>)countrycode包。

代码语言:javascript
复制
wdi <- WDI::WDI(indicator=c("SI.POV.NAHC", "SI.POV.GINI", "SL.UEM.TOTL.ZS", 
                            "SP.POP.TOTL"), start=1991, end=2018)

当您将"iso2c"用于<from><to>时,您可以很容易地使用is.na获得NA。然而,在申请之前检查结果,因为否则的话,该方法也将消除有争议的国家,如科索沃。

代码语言:javascript
复制
library(countrycode)
rm.groups <- unique(wdi$country[is.na(countrycode(wdi$iso2c, "iso2c", "iso2c"))])

代码语言:javascript
复制
rm.groups
#  [1] "Arab World"                                          
#  [2] "World"                                               
#  [3] "East Asia & Pacific (excluding high income)"         
#  [4] "Europe & Central Asia (excluding high income)"       
#  [5] "South Asia"                                          
#  [6] "Central Europe and the Baltics"                      
#  [7] "European Union"                                      
#  [8] "Fragile and conflict affected situations"            
#  [9] "Channel Islands"                                     
# [10] "OECD members"                                        
# [11] "Small states"                                        
# [12] "Pacific island small states"                         
# [13] "Caribbean small states"                              
# [14] "Other small states"                                  
# [15] "Latin America & the Caribbean (IDA & IBRD countries)"
# [16] "Middle East & North Africa (IDA & IBRD countries)"   
# [17] "East Asia & Pacific (IDA & IBRD countries)"          
# [18] "South Asia (IDA & IBRD)"                             
# [19] "Sub-Saharan Africa (IDA & IBRD countries)"           
# [20] "Europe & Central Asia (IDA & IBRD countries)"        
# [21] "Pre-demographic dividend"                            
# [22] "Early-demographic dividend"                          
# [23] "Late-demographic dividend"                           
# [24] "Post-demographic dividend"                           
# [25] "Euro area"                                           
# [26] "High income"                                         
# [27] "Heavily indebted poor countries (HIPC)"              
# [28] "IBRD only"                                           
# [29] "IDA total"                                           
# [30] "IDA blend"                                           
# [31] "IDA only"                                            
# [32] "Latin America & Caribbean (excluding high income)"   
# [33] "Kosovo"                                              
# [34] "Least developed countries: UN classification"        
# [35] "Low income"                                          
# [36] "Lower middle income"                                 
# [37] "Low & middle income"                                 
# [38] "Middle income"                                       
# [39] "Middle East & North Africa (excluding high income)"  
# [40] "Upper middle income"                                 
# [41] "North America"                                       
# [42] "Not classified"                                      
# [43] "East Asia & Pacific"                                 
# [44] "Europe & Central Asia"                               
# [45] "Sub-Saharan Africa (excluding high income)"          
# [46] "Sub-Saharan Africa"                                  
# [47] "Latin America & Caribbean"                           
# [48] "Middle East & North Africa"                          
# [49] "IDA & IBRD total" 

但这很简单。在检查rm.groups向量之后,您可能需要保留以下两个:

代码语言:javascript
复制
wdi$iso2c[wdi$country == "Kosovo"][1]
# [1] "XK"
wdi$iso2c[wdi$country == "Channel Islands"][1]
# [1] "JG"

只需像这样使用rm.groups使用%in%将它们从%in%中删除

代码语言:javascript
复制
rm.groups <- rm.groups[-which(rm.groups %in% c("Kosovo", "Channel Islands"))]

最后,您可以将这些组从wdi数据框架中删除,方法是保留那些属于!而不是%in% rm.groups的国家行。

代码语言:javascript
复制
wdi.nogroups <- wdi[!wdi$country %in% rm.groups, ]
head(wdi.nogroups)
#     iso2c country year SI.POV.NAHC SI.POV.GINI SL.UEM.TOTL.ZS SP.POP.TOTL
# 141    AD Andorra 1991          NA          NA             NA       56671
# 142    AD Andorra 1992          NA          NA             NA       58888
# 143    AD Andorra 1993          NA          NA             NA       60971
# 144    AD Andorra 1994          NA          NA             NA       62677
# 145    AD Andorra 1995          NA          NA             NA       63850
# 146    AD Andorra 1996          NA          NA             NA       64360
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63499764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档