How to import multiple data files (the fast way)

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.

Advertisements

Author: inside data blog

data analysis & visualization blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s