Ben Norris
Ben Norris

Reputation: 55

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

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!

Upvotes: 3

Views: 4250

Answers (1)

cdeterman
cdeterman

Reputation: 19960

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.

Upvotes: 6

Related Questions