6.2 C
London
Thursday, December 19, 2024
HomeRImport & Export Data in RHow to Read Zip Files in R (With Example)

How to Read Zip Files in R (With Example)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories