How to import multiple data files (the fast way)

[This article was first published on R – Inside data, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

csv-logo

Sometimes you have your data stored in multiple csv files and want to load them into a single data frame in R. There are several answers on the web to this questions and I recently found a fast solution to this problem.

# packages
require(data.table)

# set wd
setwd("PathToYourFolder")

# import files
files = list.files(pattern="*.csv")
dataset = do.call(rbind, lapply(files, fread))
rm(files)

# transform data to df
dataset <- as.data.frame(unclass(dataset))

The code above uses both lapply and the cool fread function from the data.table package to load in your data in a quiet fast manner. I recommend to try out this approach if you dealing with long import times.

Make sure you check out my Github for other data driven projects.

To leave a comment for the author, please follow the link and comment on their blog: R – Inside data.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)