25

I have a folder containing 332 csv files. The name of the files are as follows 001.csv, 002.csv, 003.csv, ............ , 330.csv, 331.csv , 332.csv . All the files have same number of variables and same format.

I need to read all the files in one dataframe. I have been reading each one and then using rbind, but this is too cumbersome.

Need help.

Madhumita
  • 489
  • 1
  • 7
  • 15
  • 1
    Although the duplicate will give you a good idea from what to do, this exact problem has been posted before in other questions [like this one](http://stackoverflow.com/questions/23951541/importing-multiple-csv-file-into-r-by-names-of-file/23952165#23952165). – Jaap Jul 18 '14 at 08:06
  • I too, have 332 csv files in my Coursera assigment ;) – user1535147 May 29 '21 at 14:27

3 Answers3

48

Try lapply and do.call

file_names <- dir() #where you have your files

your_data_frame <- do.call(rbind,lapply(file_names,read.csv))
Mario Fajardo
  • 620
  • 4
  • 8
  • This is helpful. Just to add a reminder: include "full.names = TRUE" in dire() if only folder names were read. – LCheng Mar 30 '22 at 20:16
18

Solution with data.table, answer is taken from another post in SO which I used sometimes back.

library(data.table)  
files <- list.files(path = "/etc/dump",pattern = ".csv")
temp <- lapply(files, fread, sep=",")
data <- rbindlist( temp )
Arun
  • 116,683
  • 26
  • 284
  • 387
  • 1
    How to capture the name of the files as well in the data frame in a separate column to identify the source of the data? – RanonKahn Sep 21 '19 at 05:41
  • @Arun I think you might need to add 2x backslashes before the `.csv` in your answer. – Martin Sep 01 '21 at 17:12
1

Here is a possible solution. Could probably also be done with a apply function.

path <- "path_to_files"
files <- c(paste("00",2:9,".csv",sep=""),
           paste("0",10:99,".csv",sep=""), 
           paste(100:332,".csv",sep="")
           )
#Read first file to create variables in a data frame
data <- read.csv(paste(path,"001.csv",sep="/"))

#Read remaining files and rbind them to dataset
for (f in files) {
   data <- rbind(data,read.csv(paste(path, files, sep="/")))
}
MBDK
  • 11
  • 2
  • Thank you all for your immediate help. The following commands with header=TRUE worked for me. file_names <- dir() #where you have your files your_data_frame <- do.call(rbind,lapply(file_names,read.csv, header=TRUE)) – Madhumita Jul 21 '14 at 08:09