To read multiple files from a directory and save to a data frame

June 23, 2015

(This article was first published on R – My thoughts & learnings, and kindly contributed to R-bloggers)

There are various solution to this questions like these but I will attempt to answer the problems that I encountered with there working solution that either I found or created by my own.
Question 1: My initial problem was how to read multiple .CSV files and store them into a single data frame.
Solution: Use a lapply() function and rbind(). One of the working R code I found here provided by Hadley. The code is

# The following code reads multiple csv files into a single data frame
load_data <- function(path) { 
 files <- dir(path, pattern = '\\*.csv', full.names = TRUE)
 tables <- lapply(files, read.csv), tables)

And then use the function like

> load_data("D://User//Temp")

Filed under: pre-processing, R Tagged: data munging, R

To leave a comment for the author, please follow the link and comment on their blog: R – My thoughts & learnings. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)