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

前端 未结 8 1213
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:45

    For Mac (and I assume Linux)...

    If the zip archive contains a single file, you can use the bash command funzip, in conjuction with fread from the data.table package:

    library(data.table)
    dt <- fread("curl http://www.newcl.org/data/zipfiles/a1.zip | funzip")
    

    In cases where the archive contains multiple files, you can use tar instead to extract a specific file to stdout:

    dt <- fread("curl http://www.newcl.org/data/zipfiles/a1.zip | tar -xf- --to-stdout *a1.dat")
    

提交回复
热议问题