Reading multiple csv files from a folder into a si

2019-01-17 04:38发布

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.

标签: r csv import
3条回答
姐就是有狂的资本
2楼-- · 2019-01-17 04:47

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="/")))
}
查看更多
放荡不羁爱自由
3楼-- · 2019-01-17 04:49

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 )
查看更多
Explosion°爆炸
4楼-- · 2019-01-17 04:52

Try lapply and do.call

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

your_data_frame <- do.call(rbind,lapply(file_names,read.csv))
查看更多
登录 后发表回答