用R下載World Bank和World Development Indicators的資料
library(remotes)
remotes::install_github("nset-ornl/wbstats")
install_github('vincentarelbundock/WDI')
install.packages("wbstats")
install.packages("WDI")
library(wbstats)
library(WDI)
#首先要先確定你想要下載的變數,是在World Bank(wbstats)還是在World Development Indicators(WDI)?GDP、人口這些總體資料,大部分放在wbstats,其他像是有無電力、小學畢業人數等就是World Development Indicators的資料。換句話說,你如果用wb()這個function去下載電力資料,R會回傳錯誤,不是說你沒有許可,不然就是說time out.例如出現下面這行字
Error in curl::curl_fetch_memory(url, handle = handle) :
Timeout was reached: [api.worldbank.org] Operation timed out after 20013 milliseconds with 2014253 out of 2109181 bytes received
這個問題,我一開始還以為是world bank的資料庫限制單次下載的數據量,要求要多隔幾秒鐘再下載之類的。結果後來才發現同學是要下載WDI的變數,但誤用成wb()。
########R####################
dataset_population <- wb(indicator = "SP.POP.TOTL", #world bank 官網有個id名稱
startdate = 1991,
enddate = 2020,
country = c("MK","BA","AL","SRB","HR","ME","KS"))%>% #我只需要這幾個國家
dplyr::mutate(value=value/1000000) #我把人口數的單位改成百萬
expenses_indicators<-c(elec_access="EG.ELC.ACCS.ZS",
Renewable_energy_percent="EG.ELC.RNEW.ZS",
Freshwater_percent="ER.H2O.FWTL.ZS",
health="SH.XPD.CHEX.GD.ZS",
Agricultural_irrigated_percent= "AG.LND.IRIG.AG.ZS",
Expense_percent_gdp="GC.XPN.TOTL.GD.ZS"
)
wdi <- WDI(indicator=expenses_indicators,
country = "all",
start = 1960,
end = 2020)
write.csv(wdi,"wdi.csv", na=".",row.names = TRUE, fileEncoding = "UTF-8")
Comments
Post a Comment