140

Twitter 上的@EZGraphs 写道:“很多在线 csv 文件都被压缩了。有没有办法下载、解压缩存档并使用 R 将数据加载到 data.frame 中?#Rstats”

我今天也尝试这样做,但最终只是手动下载了 zip 文件。

我试过类似的东西:

fileName <- "http://www.newcl.org/data/zipfiles/a1.zip"
con1 <- unz(fileName, filename="a1.dat", open = "r")

但我觉得我的路还很长。有什么想法吗?

4

9 回答 9

197

Zip 档案实际上更像是一个包含内容元数据等的“文件系统”。有关详细信息,请参阅help(unzip)。所以要做你上面画的你需要

  1. 创建一个温度。文件名(例如tempfile()
  2. 用于download.file()将文件提取到临时文件中。文件
  3. 用于unz()从 temp 中提取目标文件。文件
  4. 通过删除临时文件unlink()

在代码中(感谢基本示例,但这更简单)看起来像

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

压缩 ( .z) 或 gzipped ( .gz) 或 bzip2ed ( .bz2) 文件只是您可以从连接中直接读取的文件。所以让数据提供者使用它:)

于 2010-06-16T13:57:14.953 回答
29

只是为了记录,我尝试将 Dirk 的答案翻译成代码:-P

temp <- tempfile()
download.file("http://www.newcl.org/data/zipfiles/a1.zip",temp)
con <- unz(temp, "a1.dat")
data <- matrix(scan(con),ncol=4,byrow=TRUE)
unlink(temp)
于 2010-06-16T15:29:58.537 回答
22

我使用了位于http://cran.r-project.org/web/packages/downloader/index.html的 CRAN 包“下载器” 。容易得多。

download(url, dest="dataset.zip", mode="wb") 
unzip ("dataset.zip", exdir = "./")
于 2014-12-08T22:57:44.780 回答
12

对于 Mac(我假设是 Linux)...

如果 zip 存档包含单个文件,您可以使用 bash 命令funzip,与包中的结合fread使用data.table

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

如果存档包含多个文件,您可以改用tar将特定文件提取到标准输出:

dt <- fread("curl http://www.newcl.org/data/zipfiles/a1.zip | tar -xf- --to-stdout *a1.dat")
于 2016-06-15T00:19:46.603 回答
10

这是一个适用于无法使用该read.table函数读取的文件的示例。此示例读取 .xls 文件。

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))
于 2017-08-16T10:09:10.927 回答
5

要使用 data.table 执行此操作,我发现以下方法有效。不幸的是,该链接不再起作用,因此我使用了另一个数据集的链接。

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)

我知道这在一行中是可能的,因为您可以将 bash 脚本fread传递到fread.

于 2017-02-02T17:03:32.453 回答
4

试试这个代码。这个对我有用:

unzip(zipfile="<directory and filename>",
      exdir="<directory where the content will be extracted>")

例子:

unzip(zipfile="./data/Data.zip",exdir="./data")
于 2016-06-13T18:29:11.870 回答
0

我发现以下内容对我有用。这些步骤来自 BTD 的 YouTube 视频,Managing Zipfile's in R

zip.url <- "url_address.zip"

dir <- getwd()

zip.file <- "file_name.zip"

zip.combine <- as.character(paste(dir, zip.file, sep = "/"))

download.file(zip.url, destfile = zip.combine)

unzip(zip.file)
于 2020-08-09T17:27:21.697 回答
0

rio()将非常适合这个 - 它使用文件名的文件扩展名来确定它是什么类型的文件,因此它将适用于多种文件类型。我还习惯于unzip()列出 zip 文件中的文件名,因此不必手动指定文件名。

library(rio)

# create a temporary directory
td <- tempdir()

# create a temporary file
tf <- tempfile(tmpdir=td, fileext=".zip")

# download file from internet into temporary location
download.file("http://download.companieshouse.gov.uk/BasicCompanyData-part1.zip", tf)

# list zip archive
file_names <- unzip(tf, list=TRUE)

# extract files from zip file
unzip(tf, exdir=td, overwrite=TRUE)

# use when zip file has only one file
data <- import(file.path(td, file_names$Name[1]))

# use when zip file has multiple files
data_multiple <- lapply(file_names$Name, function(x) import(file.path(td, x)))

# delete the files and directories
unlink(td)
于 2021-02-24T13:09:18.360 回答