[ACCEPTED]-Using R to download zipped data file, extract, and import .csv-download

Accepted answer
Score: 23

In order to get your data to download and 6 uncompress, you need to set mode="wb"

download.file("...",temp, mode="wb")
unzip(temp, "gbr_Country_en_csv_v2.csv")
dd <- read.table("gbr_Country_en_csv_v2.csv", sep=",",skip=2, header=T)

It looks like 5 the default is "w" which assumes a text 4 files. If it was a plain csv file this would 3 be fine. But since it's compressed, it's 2 a binary file, hence the "wb". Without the 1 "wb" part, you can't open the zip at all.

Score: 5

It's almost everything ok. In this case 3 you only need to specify that it's a comma 2 separated file, eg using sep="," in read.table:

temp <- tempfile()
download.file("http://api.worldbank.org/v2/en/country/gbr?downloadformat=csv", 
              temp)
con <- unz(temp, "gbr_Country_en_csv_v2.csv")
dat <- read.table(con, header=T, skip=2, sep=",")
unlink(temp)

With this 1 little change i can import your csv smoothly.

HTH, Luca

Score: 5

The Word Bank Developmet Indictors can be 2 obtained using the WDI package. For example,

library(WDI)
inds <- WDIsearch(field = "indicator")[, 1]
GB <- WDI("GB", indicator = inds)

See WDIsearch and 1 WDI functions and the rerference manual for more info.

More Related questions