Import multiple files to R

I have been recently asked a few times how you can import a bunch of data (let’s say for example .csv files) to your R-Environment without copying and pasting a lot of code. I’m not aware of a built-in-package in R that does that for you (although I can imagine that somewhere there might be one) but I will show a little example on how you can do this manually. The advantage is that you can easily modify the code to import other file-types and if you are a beginner with R you might get some feeling for automating, loops and lists in R.

The main idea behind the following code is, that you put all your files into one directory and read them into R with a loop. Therefore you will have to work with lists which serve as a “container” to receive the incoming data. Note that your .csv files need to have the same characteristics in order to automate the process. If you have for example  csv. files with different separators (one with commas and another with semicolons) the import will not work as expected. If you have never imported data into R before, try the read functions on single files before you go to automating.

The code is quite self-explanatory:

## import_multiple_csv_files_to_R
# Purpose: Import multiple csv files to the Global Environment in R

# set working directory

# list all csv files from the current directory
list.files(pattern=".csv$") # use the pattern argument to define a common pattern  for import files with regex. Here: .csv

# create a list from these files

# create an empty list that will serve as a container to receive the incoming files<-list()

# create a loop to read in your data
for (i in 1:length(list.filenames))

# add the names of your data to the list

# now you can index one of your tables like this$deforestation2010.csv

# or this[1]

# you can make a function out of this
tmp.list.1<-list.files(mypath, pattern=mypattern)
for (i in 1:length(tmp.list.1)){tmp.list.2[[i]]<-read.csv(tmp.list.1[i],...)}

# use it like this
# note: with ... we enable the function to refine the import with parameters from read.csv.
# here we define the separator of entries in the csv files to be comma.

# save it to the folder with your custom functions

# load it like this whenever you need it in another script with

# end

6 thoughts on “Import multiple files to R

  1. Thank you for the script. How can I do the same import by importing only specific files, depending on their names?
    I have a list of plots and I only want to import the .txt files of these plots (and not the others that doesn’t concern me)?

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s