Reading an Excel file into an R dataframe from a zipped folder

馋奶兔 提交于 2021-02-10 07:45:30

问题


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.

When I type in the following syntax:

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

I get the following error messages:

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

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.

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


回答1:


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.

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")

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



来源:https://stackoverflow.com/questions/26763377/reading-an-excel-file-into-an-r-dataframe-from-a-zipped-folder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!