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

前端 未结 8 1420
Happy的楠姐
Happy的楠姐 2020-11-22 14:12

@EZGraphs on Twitter writes: \"Lots of online csvs are zipped. Is there a way to download, unzip the archive, and load the data to a data.frame using R? #Rstats\"

I

8条回答
  •  不知归路
    2020-11-22 14:35

    Zip archives are actually more a 'filesystem' with content metadata etc. See help(unzip) for details. So to do what you sketch out above you need to

    1. Create a temp. file name (eg tempfile())
    2. Use download.file() to fetch the file into the temp. file
    3. Use unz() to extract the target file from temp. file
    4. Remove the temp file via unlink()

    which in code (thanks for basic example, but this is simpler) looks like

    temp <- tempfile()
    download.file("http://www.newcl.org/data/zipfiles/a1.zip",temp)
    data <- read.table(unz(temp, "a1.dat"))
    unlink(temp)
    

    Compressed (.z) or gzipped (.gz) or bzip2ed (.bz2) files are just the file and those you can read directly from a connection. So get the data provider to use that instead :)

提交回复
热议问题