从压缩文件夹中将 Excel 文件读入 R 数据帧 [英] Reading an Excel file into an R dataframe from a zipped folder

查看:25
本文介绍了从压缩文件夹中将 Excel 文件读入 R 数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Excel 文件(.xls 扩展名),它位于一个压缩文件夹中,我想将它作为数据帧读入 R.我加载了 gdata 库并将我的工作目录设置为包含压缩文件夹的文件夹.

I have an Excel file (.xls extension) that is inside a zipped folder that I would like to read as a dataframe into R. I loaded the gdata library and set up my working directory to the folder that houses the zipped folder.

当我输入以下语法时:

data_frame1 <- read.xls( unz("./Data/Project1.zip","schools.xls"))

我收到以下错误消息:

path.expand(xls) 中的错误:'path' 参数无效

Error in path.expand(xls) : invalid 'path' argument

file.exists(tfn) 中的错误:'file' 参数无效

Error in file.exists(tfn) : invalid 'file' argument

我猜我在语法中遗漏了一些参数,但我不完全确定还需要包含什么.

I'm guessing that I'm missing some arguments in the syntax, but I'm not entirely sure what else needs to be included.

感谢您的帮助!这个R新手真的很感激!

Thanks for your help! This R newbie really appreciates it!

推荐答案

遗憾的是,在快速浏览了我所知道的所有 xls 函数后,没有能够识别 unz 输出的 xls 读取函数(我很想在这里被证明是错误的).如果它是'csv',它会正常工作.按照目前的情况,在编写这样的函数之前,您必须分两步进行加载,然后提取然后加载.

Unfortunately, after a quick survey of all the xls functions I know, there is no xls reading function that can recognize the unz output (I would love to be proven wrong here). If it were a 'csv' it would work fine. As it stands, until such a function is written, you must do the loading in two steps extraction and then loading.

为了给您更多的控制权,您可以使用 unzip 指定要解压缩的文件以及放置文件的目录.

To give you a little more control, you can specify which file to unzip as well as the directory to place the files with unzip.

# default exdir is current directory
unzip(zipfile="./Data/Project1.zip", files = "schools.xls", exdir=".")

dataframe_1 <- read.xls("schools.xls")

遗憾的是,这也意味着如果您不想让 'xls' 文件四处闲逛,您必须在事后进行清理.

Sadly, this also means that you must do cleanup afterwards if you don't want the 'xls' file hanging around.

这篇关于从压缩文件夹中将 Excel 文件读入 R 数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆