Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Reading in a zip data file without unzipping it

Tags:

r

I have a very large zip file and i am trying to read it into R without unzipping it like so:

temp <- tempfile("Sales", fileext=c("zip")) data <- read.table(unz(temp, "Sales.dat"), nrows=10, header=T, quote="\"", sep=",")  Error in open.connection(file, "rt") : cannot open the connection In addition: Warning message: In open.connection(file, "rt") :   cannot open zip file 'C:\Users\xxx\AppData\Local\Temp\RtmpyAM9jH\Sales13041760345azip' 
like image 990
laiboonh Avatar asked Sep 17 '12 14:09

laiboonh


People also ask

Can R read from zip?

To read a zip file and extract data from it to R environment, we can use the download. file() to download the zip, then unzip() allows to unzip the same and extract files using read.

How can I view a zip file without downloading it?

Install the Chrome extension from the Chrome Web Store. Or open your Google Drive account > New > More > Connect more apps. Search for ZIP Extractor and install it. The ZIP Extractor screen gives you the option to select the files to extract.

Can R read compressed files?

R user John Christie points out a handy feature introduced in R 2.10: you can now read directly from a text file compressed using gzip or other file-compression tools.

How do I run a zip file in R?

How to zip files in R. To zip files in R, use the zip() function. The zipped file is stored inside the current directory unless a different path is specified in the zip() function argument. The zip() method creates a new ZIP archive, and it overwrites the output file if it exists.


1 Answers

If your zip file is called Sales.zip and contains only a file called Sales.dat, I think you can simply do the following (assuming the file is in your working directory):

data <- read.table(unz("Sales.zip", "Sales.dat"), nrows=10, header=T, quote="\"", sep=",") 
like image 70
plannapus Avatar answered Sep 24 '22 11:09

plannapus