Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read a csv file in a zipped folder with R without unzipping

Tags:

r

csv

zip

I have a zipped file named master.zip which contains 2 CSV files inside it: file1.csv and file2.csv

I want to read file1.csv only, something like: read_csv('master/file1.csv'), but without having to unzip master.zip. How can I achieve this with R?

like image 931
Tue Nguyen Avatar asked Oct 11 '17 07:10

Tue Nguyen


People also ask

How do I read a zipped csv file in R?

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. csv().

Can R read Compressed files?

Instead of losing time unzipping the file manually, it's perfectly fine to load these files directly into R. Using base code, loading a compressed file containing one or two CSV files can be done using the unz function. You can even load files that are within a folder inside that ZIP file.

Can we zip csv file?

Using File Explorer, navigate to the folder where the CSV import files reside, and select the files you want to compress and zip. For Watson Recruitment, don't forget to include the Properties file, if you use one. Right-click. From the resulting menu, select Send To, and then select Compressed (Zipped) Folder.


1 Answers

You just need to use the native function unz(). Let's assume that master.zip is in your working directory,

# just a list of files inside master.zip
master <- as.character(unzip("master.zip", list = TRUE)$Name)
# load the first file "file1.csv"
data <- read.csv(unz("master.zip", "file1.csv"), header = TRUE,
                 sep = ",") 
like image 179
nghauran Avatar answered Sep 17 '22 18:09

nghauran