Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading multiple csv files from a folder into a single dataframe in R [duplicate]

Tags:

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.

like image 419
Madhumita Avatar asked Jul 18 '14 07:07

Madhumita


2 Answers

Try lapply and do.call

file_names <- dir() #where you have your files  your_data_frame <- do.call(rbind,lapply(file_names,read.csv)) 
like image 126
Mario Fajardo Avatar answered Oct 11 '22 20:10

Mario Fajardo


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 ) 
like image 26
on_the_shores_of_linux_sea Avatar answered Oct 11 '22 20:10

on_the_shores_of_linux_sea