How Do I Read in Multiple Files in R

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.

Tabular array of contents:

Let's accept 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                <-                information.                frame                (x1                =                i                :                5,                # First information frame                x2                =                messages[                1                :                5                ]                )                data2                <-                data.                frame                (y1                =                1                :                v,                # Second data frame                y2                =                letters[                1                :                5                ]                )                data3                <-                data.                frame                (z1                =                1                :                5,                # Third information frame                z2                =                letters[                1                :                5                ]                )              

data1 <- data.frame(x1 = 1:5, # First data frame x2 = letters[1:5]) data2 <- information.frame(y1 = 1:5, # Second data frame y2 = messages[i:5]) data3 <- data.frame(z1 = 1:5, # Third data frame z2 = letters[one:5])

We also have to create a directory binder on our computer were we can store our data equally CSV files. For this, we can employ 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 accept 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.

Instance 1: Writing Multiple CSV Files to Binder Using for-Loop

In this Example, I'll testify how to export multiple information frames from R to a directory using a for-loop. Get-go, 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"

At present, we tin can run a for-loop that writes all our information frames to a folder using the write.csv2 function as shown below. Inside the for-loop, we are specifying the names of our information frames wrapped by the get office and our directory path:

                for                (i                in                one                :length(data_names)                )                {                # Caput of for-loop                write.                csv2                (                become                (data_names[i]                ),                # Write CSV files to binder                paste0(                "C:/Users/Joach/Desktop/My Binder/",                     data_names[i],                ".csv"                ),              row.                names                =                Fake                )                }              

for(i in one:length(data_names)) { # Head of for-loop write.csv2(go(data_names[i]), # Write CSV files to binder paste0("C:/Users/Joach/Desktop/My Folder/", data_names[i], ".csv"), row.names = Imitation) }

multiple csv files to folder for-loop r

Figure 1 shows how our folder should look like later running the previous R codes. In the folder, you tin can see iii CSV files.

Case 2: Reading Multiple CSV Files from Folder Using for-Loop

Case two illustrates how to import multiple CSV files using a for-loop in R. First, we have to utilise the list.files function to extract all file names in our binder:

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 # Impress file names # "data1.csv" "data2.csv" "data3.csv"

At present, nosotros 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 information frames                read.                csv2                (paste0(                "C:/Users/Joach/Desktop/My Folder/",                    data_files[i]                )                )                )                }              

for(i in 1:length(data_files)) { # Caput 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 information frame                #   y1 y2                # 1  1  a                # 2  ii  b                # iii  3  c                # iv  iv  d                # 5  five  eastward                data3                # Print third information frame                #   z1 z2                # 1  1  a                # 2  2  b                # iii  3  c                # iv  4  d                # v  v  east              

data1 # Print commencement data frame # x1 x2 # one i a # 2 2 b # 3 3 c # 4 iv d # 5 5 eastward data2 # Print second data frame # y1 y2 # i 1 a # two ii b # iii 3 c # 4 iv d # 5 5 due east data3 # Impress third data frame # z1 z2 # 1 ane a # ii 2 b # 3 3 c # four 4 d # 5 five e

Video, Farther Resources & Summary

Accept a look at the post-obit video of my YouTube channel. I testify 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 virtually for-loops below.

  • for-Loop in R
  • Loops in R
  • Bank 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 commodity illustrated how to read and write CSVs in loops in the R programming language. If yous take further questions, don't hesitate to let me know in the comments.

blackhishadinin.blogspot.com

Source: https://statisticsglobe.com/r-write-read-multiple-csv-files-for-loop

0 Response to "How Do I Read in Multiple Files in R"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel