You can read zip files in R using unzip() function from readr package.
Method: Use unzip() function
library(readr)
df <- read_csv(unzip("file.zip","data.csv"))
Let’s understand this function using example.
Using unzip() function
Let’s assume I have product.zip file in my working directory. Follow below syntax to show all files in ZIP:
# Get list of all files in zip folder
files = unzip("product.zip", list = TRUE)
# Print all files in zip folder
print(files)
Output :
Name Length Date
1 Machine_data.csv 309 2023-12-19 21:49:00
2 Machine_data.xlsx 5346 2023-12-19 22:06:00
3 product.csv 232 2023-12-21 11:09:00
4 R topics.xlsx 86073 2023-12-27 17:24:00
As output show list of files with respective it’s length and date they were created.
With the read_csv() function from the readr package, you can read a CSV file from ZIP:
# Import library
library(readr)
# Get all files in zip folder
data = read_csv(unzip("product.zip", "Machine_data.csv"))
# Print data from CSV file
print(data)
Output:
Date Machine_type Status Pressure Temperature
<chr> <chr> <lgl> <dbl> <dbl>
1 21-10-2023 Mach_1 TRUE 11 45.6
2 22-10-2023 Mach_2 TRUE 12 47.2
3 23-10-2023 Mach_3 FALSE 10 46.1
4 24-10-2023 Mach_4 TRUE 11 46.8
5 01-11-2023 Mach_A FALSE 89 10.1
6 02-11-2023 Mach_B TRUE 85 12.8
7 03-11-2023 Mach_C FALSE 87 13.7
8 04-11-2023 Mach_D FALSE 90 9.55
The below output shows data from Machine_data.csv file from ZIP files.