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?
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().
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.
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.
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 = ",")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With