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

前端 未结 8 1417
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条回答
  •  萌比男神i
    2020-11-22 14:25

    Here is an example that works for files which cannot be read in with the read.table function. This example reads a .xls file.

    url <-"https://www1.toronto.ca/City_Of_Toronto/Information_Technology/Open_Data/Data_Sets/Assets/Files/fire_stns.zip"
    
    temp <- tempfile()
    temp2 <- tempfile()
    
    download.file(url, temp)
    unzip(zipfile = temp, exdir = temp2)
    data <- read_xls(file.path(temp2, "fire station x_y.xls"))
    
    unlink(c(temp, temp2))
    

提交回复
热议问题