Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to zip multiple CSV files in R?

Tags:

dataframe

r

csv

zip

I am trying to zip multiple CSV files in R. Below is the code for reference.

# Create two dataframes using inbuilt datasets for reproducible code
df1 <- head(mtcars)
df2 <- head(iris)

# Write the files as CSV into working directory
write.csv(df1, file = "Test_File1.csv", row.names = FALSE, quote = FALSE)
write.csv(df2, file = "Test_File2.csv", row.names = FALSE, quote = FALSE)

# Read the 2 CSV file names from working directory
Zip_Files <- list.files(path = getwd(), pattern = ".csv$")

# Zip the files and place the zipped file in working directory
zip(zipfile = "TestZip", files = Zip_Files)

I am getting the below warning message. The Zip file has not been created.

Warning message:
running command '"zip" -r9X "TestZip" "Test_File1.csv" "Test_File2.csv" ' had status 127

I even tried this command to read CSV file names: Zip_Files <- list.files(path = getwd(), pattern = ".csv$", full.names = TRUE) But I still get the warning message shown above. I already have WinRAR and 7-Zip installed in my computer. I am using the latest version of R (3.4.2 64 Bit) along with latest version of RStudio. I have a Windows 7 x64 OS. Any help on this would be really appreciated.

like image 475
Code_Sipra Avatar asked Nov 18 '17 20:11

Code_Sipra


People also ask

How do I zip a csv file in R?

The zip = "C:\\Program Files\\7-Zip\\7Z" argument tells R what program to use perform the compression. In this case, I pointed it at 7Z, the command line version of 7Zip, but you can use other command line programs by changing this to point to a different program.

How do I zip in R?

To zip files in R, use the zip() function. The zipped file is stored inside the current directory unless a different path is specified in the zip() function argument. The zip() method creates a new ZIP archive, and it overwrites the output file if it exists.

Can you upload a zip file to R?

Instead of losing time unzipping the file manually, it's perfectly fine to load these files directly into R. Using base code, loading a compressed file containing one or two CSV files can be done using the unz function. You can even load files that are within a folder inside that ZIP file.

How to import and merge multiple CSV files in R?

Import & Merge Multiple csv Files in R (Example) 1 Exemplifying Data. Before we can start with the example, we need to create an exemplifying directory including multiple csv files. 2 Import & Load csv Files in R. We need three R add-on packages for the following R syntax: dplyr, plyr, and readr. ... 3 Video & Further Resources. ...

How to create a CSV file in R?

First, we’ll have to construct some exemplifying data frames in R: We also have to create a directory folder on our computer were we can store our data as CSV files. For this, we can use the dir.create function as shown below: Note that you have to replace the previously used directory path by your own path.

How to import multiple CSV files using a for-loop in R?

In the folder, you can see three CSV files. Example 2 illustrates how to import multiple CSV files using a for-loop in R. First, we have to use the list.files function to extract all file names in our folder: Now, we can write a for-loop containing the assign, paste0, and read.csv2 functions to read and save all files in our directory:

How to list all files in folder with extension CSV in R?

I set the directory in R and used the function list.files to list all files in folder with extension CSV. In the R Studio environment, I have only the location of CSV files; no file is uploaded yet. To upload all files and create a dataset will use ldply and applied the read_csv function.


2 Answers

The problem is that R's zip does not actually have code to zip (compress) files. It calls an external program to do that. You must let zip know what program to use and what arguments to give that program. You should be able to make this work like this:

zip(zipfile = "TestZip", files = Zip_Files, flags = " a -tzip",
    zip = "C:\\Program Files\\7-Zip\\7Z")

If your path to 7Z, the command line version of 7Zip, is different, please adjust to match your installation.

Some explanation:

The zip = "C:\\Program Files\\7-Zip\\7Z" argument tells R what program to use perform the compression. In this case, I pointed it at 7Z, the command line version of 7Zip, but you can use other command line programs by changing this to point to a different program.

The flags = " a -tzip" argument depends on the program that you are using. I set this up for 7Z. Reading the 7Z documentation you will see that you need to give 7Z a command (the "a") and flags (the "-tzip"). The "a" command means add these files to the archive. The -tzip flag means make it a zip archive instead of a 7Z archive. With different programs, you would need to read the documentation and construct appropriate flags for that program.

Update: If you need to have this functionality on diverse customer machines, you should consider looking into the zip package It does not require any external program and provides similar functionality.

like image 56
G5W Avatar answered Oct 06 '22 19:10

G5W


you could install the zip package and use it in your code . That way, anybody using your code would be able to zip the files without installing or searching to configure and this works for any OS.

library(zip)

# Create two dataframes using inbuilt datasets for reproducible code
df1 <- head(mtcars)
df2 <- head(iris)

# Write the files as CSV into working directory
write.csv(df1, file = "Test_File1.csv", row.names = FALSE, quote = FALSE)
write.csv(df2, file = "Test_File2.csv", row.names = FALSE, quote = FALSE)

# Read the 2 CSV file names from working directory
Zip_Files <- list.files(path = getwd(), pattern = ".csv$")

# Zip the files and place the zipped file in working directory
zip::zip(zipfile = "TestZip", files = Zip_Files)
like image 35
BenoitLondon Avatar answered Oct 06 '22 19:10

BenoitLondon