如何在R中通过JSON下载IMF数据
我最近对通过JSON检索R中的数据感兴趣。具体来说,我希望能够通过国际货币基金组织获取数据。我对JSON几乎一无所知,因此我将分享我[认为我]到目前为止所知道的,以及我所取得的成就 我在他们的网页上浏览JSON,这有点帮助。它给了我一个起点URL。这是网页<代码>http://datahelp.imf.org/knowledgebase/articles/667681-using-json-restful-web-service 我设法下载了一些列表(使用如何在R中通过JSON下载IMF数据,json,r,database,api,Json,R,Database,Api,我最近对通过JSON检索R中的数据感兴趣。具体来说,我希望能够通过国际货币基金组织获取数据。我对JSON几乎一无所知,因此我将分享我[认为我]到目前为止所知道的,以及我所取得的成就 我在他们的网页上浏览JSON,这有点帮助。它给了我一个起点URL。这是网页http://datahelp.imf.org/knowledgebase/articles/667681-using-json-restful-web-service 我设法下载了一些列表(使用GET()和fromJSON()函数),这些列表
GET()
和fromJSON()
函数),这些列表非常庞大。我对列表了解得足够多,所以“呼叫”成功了,但我一生都无法获得实际数据。到目前为止,我一直在尝试对“内容”数据使用rawToChar()
函数,但实际上我被困在那里了
如果有什么不同的话,我设法创建了包含代码的数据帧,我认为这些代码将在JSON链接的某个地方使用。这是我的
all.imf.data = fromJSON("http://dataservices.imf.org/REST/SDMX_JSON.svc/Dataflow/")
str(all.imf.data)
#all.imf.data$Structure$Dataflows$Dataflow$Name[[2]] #for the catalogue of sources
catalogue1 = cbind(all.imf.data$Structure$Dataflows$Dataflow$KeyFamilyRef,
all.imf.data$Structure$Dataflows$Dataflow$Name[[2]])
catalogue1 = catalogue1[,-2] # catalogue of all the countries
data.structure = fromJSON("http://dataservices.imf.org/REST/SDMX_JSON.svc/DataStructure/IFS")
info1 = data.frame(data.structure$Structure$Concepts$ConceptScheme$Concept[,c(1,4)])
View(data.structure$Structure$CodeLists$CodeList$Description)
str(data.structure$Structure$CodeLists$CodeList$Code)
#Units
units = data.structure$Structure$CodeLists$CodeList$Code[[1]]
#Countries
countries = data.frame(data.structure$Structure$CodeLists$CodeList$Code[[3]])
countries = countries[,-length(countries)]
#Series Codes
codes = data.frame(data.structure$Structure$CodeLists$CodeList$Code[[4]])
codes = codes[,-length(codes)]
# all.imf.data # JSON from the starting point, provided on the website
# catalogue1 # data frame of all the data bases, International Financial Statistics, Government Financial Statistics, etc.
# codes # codes for the specific data sets (GDP, Current Account, etc).
# countries # data frame of all the countries and their ISO codes
# data.structure # large list, with starting URL and endpoint "IFS". Ideally, I want to find some data set somewhere within this data base.
"info1" # looks like parameters for retrieving the data (for instance, dates, units, etc).
# units # data frame that indicates the options for units
我想要一些关于如何检索任何数据的建议,比如固定年份的GDP(PPP)。我一直在关注R博客上的一篇文章(该文章检索了欧盟数据库中的数据),但我无法为IMF复制这一过程。我觉得我很快就能找到一些有用的东西了,但我不能完全做到这一点。考虑到我有包含数据库名称、序列和序列代码的数据帧,我认为这只是一个如何构造适当的URL来获取数据的问题,但我可能错了
数据框中提供的代码是我假定的数据集的代码。有没有一种方法可以调用(比如)美国的
BK\u DB\u BP6\u USD
数据,即“国际收支、资本账户、总额、借方等”?在R的上下文中,我应该如何做这件事 也许只要使用API并尝试在不太复杂的数据源上学习JSON即可或者-阅读现有API pkg的源代码。哦,是的!我知道那个包裹!谢谢然而,考虑到许多网站通过JSON共享信息,我认为完成此练习对于我使用不太复杂的网站和/或阅读源代码会很有用。也许只需使用API并尝试在不太复杂的数据源上学习JSON即可。-或者-阅读现有API pkg的源代码。哦,是的!我知道那个包裹!谢谢然而,考虑到许多网站通过JSON共享信息,我认为完成这个练习对于我使用不太复杂的网站和/或阅读源代码会很有用。