How can I read zip files in R, and can you provide an example?

How can I read zip files in R, and can you provide an example?

Reading zip files in R is a convenient way to access and extract data files that have been compressed into a single zip file. This can be done using the “unzip” function in R, which allows you to specify the path to the zip file and the location where you want the extracted files to be saved. An example of this would be:

unzip(“C:/Users/Documents/data.zip”, exdir = “C:/Users/Documents/extracted_data”)

This command would extract all the files within the “data.zip” file and save them in the specified location. It is important to note that the “unzip” function only extracts the files, and you would still need to use other functions or packages in R to read and manipulate the data contained within the extracted files.

Read Zip Files in R (With Example)


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 .

Additional Resources

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

Cite this article

stats writer (2024). How can I read zip files in R, and can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-read-zip-files-in-r-and-can-you-provide-an-example/

stats writer. "How can I read zip files in R, and can you provide an example?." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-read-zip-files-in-r-and-can-you-provide-an-example/.

stats writer. "How can I read zip files in R, and can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-read-zip-files-in-r-and-can-you-provide-an-example/.

stats writer (2024) 'How can I read zip files in R, and can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-read-zip-files-in-r-and-can-you-provide-an-example/.

[1] stats writer, "How can I read zip files in R, and can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I read zip files in R, and can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top