This question already has an answer here:
-
What's wrong with my function to load multiple .csv files into single dataframe in R using rbind?
5 answers
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.
Try lapply and do.call
file_names <- dir() #where you have your files
your_data_frame <- do.call(rbind,lapply(file_names,read.csv))
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 )
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="/")))
}