Write & Read Multiple CSV Files Using for-Loop in R (2 Examples)
In this R tutorial you’ll learn how to export and import multiple CSV files using a for-loop.
Table of contents:
Let’s take a look at some R codes in action:
Creation of Example Data
First, we’ll have to construct some exemplifying data frames in R:
data1 <- data.frame(x1 = 1:5, # First data frame x2 = letters[1:5]) data2 <- data.frame(y1 = 1:5, # Second data frame y2 = letters[1:5]) data3 <- data.frame(z1 = 1:5, # Third data frame z2 = letters[1:5]) |
data1 <- data.frame(x1 = 1:5, # First data frame x2 = letters[1:5]) data2 <- data.frame(y1 = 1:5, # Second data frame y2 = letters[1:5]) data3 <- data.frame(z1 = 1:5, # Third data frame z2 = letters[1:5])
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:
dir.create("C:/Users/Joach/Desktop/My Folder") # Create folder |
dir.create("C:/Users/Joach/Desktop/My Folder") # Create folder
Note that you have to replace the previously used directory path by your own path. After running the previous R code you should see a new folder on your desktop.
Example 1: Writing Multiple CSV Files to Folder Using for-Loop
In this Example, I’ll show how to export multiple data frames from R to a directory using a for-loop. First, we have to specify the names of all data frames we want to export:
data_names <- c("data1", "data2", "data3") # Create vector of names data_names # Print names # "data1" "data2" "data3" |
data_names <- c("data1", "data2", "data3") # Create vector of names data_names # Print names # "data1" "data2" "data3"
Now, we can run a for-loop that writes all our data frames to a folder using the write.csv2 function as shown below. Within the for-loop, we are specifying the names of our data frames wrapped by the get function and our directory path:
for(i in 1:length(data_names)) { # Head of for-loop write.csv2(get(data_names[i]), # Write CSV files to folder paste0("C:/Users/Joach/Desktop/My Folder/", data_names[i], ".csv"), row.names = FALSE) } |
for(i in 1:length(data_names)) { # Head of for-loop write.csv2(get(data_names[i]), # Write CSV files to folder paste0("C:/Users/Joach/Desktop/My Folder/", data_names[i], ".csv"), row.names = FALSE) }
Figure 1 shows how our folder should look like after running the previous R codes. In the folder, you can see three CSV files.
Example 2: Reading Multiple CSV Files from Folder Using for-Loop
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:
data_files <- list.files("C:/Users/Joach/Desktop/My Folder") # Identify file names data_files # Print file names # "data1.csv" "data2.csv" "data3.csv" |
data_files <- list.files("C:/Users/Joach/Desktop/My Folder") # Identify file names data_files # Print file names # "data1.csv" "data2.csv" "data3.csv"
Now, we can write a for-loop containing the assign, paste0, and read.csv2 functions to read and save all files in our directory:
for(i in 1:length(data_files)) { # Head of for-loop assign(paste0("data", i), # Read and store data frames read.csv2(paste0("C:/Users/Joach/Desktop/My Folder/", data_files[i]))) } |
for(i in 1:length(data_files)) { # Head of for-loop assign(paste0("data", i), # Read and store data frames read.csv2(paste0("C:/Users/Joach/Desktop/My Folder/", data_files[i]))) }
Our data frames are now stored in the data objects data1, data2, and data3:
data1 # Print first data frame # x1 x2 # 1 1 a # 2 2 b # 3 3 c # 4 4 d # 5 5 e data2 # Print second data frame # y1 y2 # 1 1 a # 2 2 b # 3 3 c # 4 4 d # 5 5 e data3 # Print third data frame # z1 z2 # 1 1 a # 2 2 b # 3 3 c # 4 4 d # 5 5 e |
data1 # Print first data frame # x1 x2 # 1 1 a # 2 2 b # 3 3 c # 4 4 d # 5 5 e data2 # Print second data frame # y1 y2 # 1 1 a # 2 2 b # 3 3 c # 4 4 d # 5 5 e data3 # Print third data frame # z1 z2 # 1 1 a # 2 2 b # 3 3 c # 4 4 d # 5 5 e
Video, Further Resources & Summary
Have a look at the following video of my YouTube channel. I show the R programming syntax of this tutorial in the video.
The YouTube video will be added soon.
Furthermore, you might want to read the other tutorials on this website. You can find some tutorials about for-loops below.
- for-Loop in R
- Loops in R
- Check in R if a Directory Exists and Create if It doesn’t
- Import & Merge Multiple csv Files
- List All Files with Specific Extension
- The R Programming Language
To summarize: This article illustrated how to read and write CSVs in loops in the R programming language. If you have further questions, don’t hesitate to let me know in the comments.