Home » How to Read Zip Files in R (With Example)

How to Read Zip Files in R (With Example)

by Erma Khan

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 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 working directory 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 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 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

Related Posts