相关疑难解决方法(0)

在R中自动读取zip文件

我需要自动化R来读取一个zip文件中的csv数据文件.

例如,我会输入:

read.zip(file = "myfile.zip")
Run Code Online (Sandbox Code Playgroud)

在内部,将要做的是:

  • 解压缩myfile.zip到临时文件夹
  • 使用读取其中包含的唯一文件 read.csv

如果zip文件中有多个文件,则会引发错误.

我的问题是获取包含在zip文件中的文件的名称,在orded中提供它来执行read.csv命令.有谁知道怎么做?

UPDATE

这是我根据@Paul答案写的函数:

read.zip <- function(zipfile, row.names=NULL, dec=".") {
    # Create a name for the dir where we'll unzip
    zipdir <- tempfile()
    # Create the dir using that name
    dir.create(zipdir)
    # Unzip the file into the dir
    unzip(zipfile, exdir=zipdir)
    # Get the files into the dir
    files <- list.files(zipdir)
    # Throw an error if there's more than one
    if(length(files)>1) stop("More than one data file inside zip") …
Run Code Online (Sandbox Code Playgroud)

compression r

24
推荐指数
2
解决办法
2万
查看次数

标签 统计

compression ×1

r ×1