How do I read zip files in R (with example)?

Reading zip files in R can be done easily using the read_zip() function from the readr package. To illustrate, the following code reads a zip file containing a .csv file and stores the data in the dataframe df: df <- read_zip(“my_zip_file.zip”) %>% read_csv()


You can use the following basic syntax to read a ZIP file into R:

library(readr)

#import data1.csv located within my_data.zip
df <- read_csv(unzip("my_data.zip", "data1.csv"))

The following example shows how to use this syntax in practice.

Example: How to Read Zip Files in R

Suppose I have a ZIP file called my_data.zip that contains the following three CSV files:

  • data1.csv
  • data2.csv
  • data3.csv

Assuming my contains this ZIP file, I can use the following syntax to display all files located within my_data.zip:

#display all files in my_data.zip
unzip("my_data.zip", list = TRUE)

       Name Length                Date
1 data1.csv     37 2022-03-10 09:48:00
2 data2.csv     36 2022-03-10 09:49:00
3 data3.csv     34 2022-03-10 10:54:00 

We can see the names of each file located within my_data.zip along with their length and the date they were created.

Next, I can use the following syntax to import the dataset called data1.csv into a data frame in R:

library(readr)

#read data1.csv into data frame
df1 <- read_csv(unzip("my_data.zip", "data1.csv"))

#view data frame
df1

# A tibble: 4 x 2
  team  points
    
1 A         12
2 B         31
3 C         27
4 D         30

We can see that R successfully imported this CSV file into a data frame.

Note: You can find the complete documentation for the read_csv() function .

The following tutorials explain how to import other files in R:

x