How to Read Zip Files in R (With Example)

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


#import data1.csv located within
df <- read_csv(unzip("", "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 that contains the following three CSV files:

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

Assuming my working directory contains this ZIP file, I can use the following syntax to display all files located within

#display all files in
unzip("", 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 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:


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

#view data frame

# 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 here.

Additional Resources

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

How to Import CSV Files into R
How to Import a CSV from URL in R
How to Import Excel Files into R

Leave a Reply

Your email address will not be published.