Open In App

Import and Merge Multiple CSV Files in R

In this article, we will be looking at the approach to merge multiple CSV files in the R programming language.

Modules Used

Functions Used

Syntax: list.files(path = “.”, pattern = NULL, all.files = FALSE,full.names = FALSE, recursive = FALSE, ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)



Syntax: lapply(X, FUN, …)

Syntax:



bind_rows(…, .id = NULL)

Parameter:

…: Data frames to combine.

.id: Data frame identifier.

To merge multiple CSV files, the user needs to install and import dplyr,plyr, and readr packages in the R console to call the functions which are list.files(), lapply(), and bind_rows() from these packages and pass the required parameters to these functions to merge the given multiple CSV files to a single data frame in the R programming language.

Data in Use:

Example:




library("dplyr")                                                
library("plyr")                                               
library("readr")  
  
gfg_data <- list.files(path = "C:/Users/Geetansh Sahni/Documents/R/Data"
                      pattern = "*.csv", full.names = TRUE) %>%
 lapply(read_csv) %>%                                          
 bind_rows                                                      
  
gfg_data

 Output:

Article Tags :