Reading multiple csv files from a folder into a single dataframe in R [duplicate]
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.
Solution 1:
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 2:
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 )
Solution 3:
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="/")))
}