Using R to download zipped data file, extract, and import .csv

前端 未结 3 1847
無奈伤痛
無奈伤痛 2020-12-06 01:07

I am trying to download and extract a .csv file from a webpage using R.

This question is a duplicate of Using R to download zipped data file, extrac

3条回答
  •  独厮守ぢ
    2020-12-06 01:36

    In order to get your data to download and 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 the default is "w" which assumes a text files. If it was a plain csv file this would be fine. But since it's compressed, it's a binary file, hence the "wb". Without the "wb" part, you can't open the zip at all.

提交回复
热议问题