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

前端 未结 8 1419
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:26

    To do this using data.table, I found that the following works. Unfortunately, the link does not work anymore, so I used a link for another data set.

    library(data.table)
    temp <- tempfile()
    download.file("https://www.bls.gov/tus/special.requests/atusact_0315.zip", temp)
    timeUse <- fread(unzip(temp, files = "atusact_0315.dat"))
    rm(temp)
    

    I know this is possible in a single line since you can pass bash scripts to fread, but I am not sure how to download a .zip file, extract, and pass a single file from that to fread.

提交回复
热议问题