How to import multiple .csv files simultaneously in R and create a data frame

Have you ever struggled to import hundred of small datasets files? Can be very time consuming or maybe impossible. I was in this situation some time ago when I had a folder with approximately three thousand CSV files, and I was interested in creating a single dataset.

At the time I was thinking to create a for loop for importing each file separately and then to merge all small datasets.

# file1 = read_csv("file1.csv")
# file2 = read_csv("file2.csv")
# file3 = read_csv("file3.csv")

I didn't know how that would work, or even it would be possible to merge 3000 datasets easily. Anyway, I started searching for similar questions, and I don't remember that I found something helpful until I discovered the plyr package. I am happy to share it with you. There are no many codes.

Load the package

library(plyr)
library(readr)

For this post, I created 3 CSV files and put them in a folder (i.e., cvsfolder) in my desktop. You can do the same if you want to replicate this post. I set the directory in R and used the function list.files to list all files in folder with extension CSV.

setwd("~/Desktop")
mydir = "csvfolder"
myfiles = list.files(path=mydir, pattern="*.csv", full.names=TRUE)
myfiles
## [1] "csvfolder/file1.csv" "csvfolder/file2.csv" "csvfolder/file3.csv"

In the R Studio environment, I have only the location of CSV files; no file is uploaded yet. To upload all files and create a dataset will use ldply and applied the read_csv function.

dat_csv = ldply(myfiles, read_csv)
dat_csv
##    a  b  c
## 1  1 34 98
## 2 23 55 10
## 3 43 67  3
## 4 32 21 56
## 5 34 23 57
## 6 31 24 58
## 7 43 65 77
## 8 45 63 78
## 9 57 61 79

Done!

You can apply the same function for importing .txt files as well. The function read.table shall be used for .txt files. See code below:

# dat_txt = ldply(myfiles, read.table, sep = "\t", fill=TRUE, header = TRUE)

Extra

Below I will import each file separately to show that the dataset and variable names correspondent with the dat_csv above.

read_csv(myfiles[1])
## # A tibble: 3 x 3
##       a     b     c
##   <int> <int> <int>
## 1     1    34    98
## 2    23    55    10
## 3    43    67     3
read_csv(myfiles[2])
## # A tibble: 3 x 3
##       a     b     c
##   <int> <int> <int>
## 1    32    21    56
## 2    34    23    57
## 3    31    24    58
read_csv(myfiles[3])
## # A tibble: 3 x 3
##       a     b     c
##   <int> <int> <int>
## 1    43    65    77
## 2    45    63    78
## 3    57    61    79

I hope you learned something new today and share it with your peers. Who knows it may be helpful for someone else.

5 Comments

  1. SP
    Sung-Bin Park April 13, 2021

    useful, thanks! how can I read many data from multiple subdirectories?

    Reply
  2. MT
    Mireia Tirorí January 6, 2020

    very useful! thanks!

    Reply
  3. C
    Carla August 25, 2019

    Thank you so <3

    Reply
  4. MS
    Mohammad J. Shamim July 31, 2019

    Dear Sir, I have a problem. The problem is that I have over 400 csv files where there are no headers for the columns (They start with numbers. the data is extract from a Gas exchange machine). I would like to extract the first row of each csv file and put them together in a large file that would have over 400 observations. I can merge multiple files into one using three different ways including yours, but I failed to extract the first row of the files and combine them. Do you have any idea? I will really appreciate it.

    Reply
  5. KB
    Kunal Bali June 12, 2019

    importing netcdf files in R and mapping using ggplot? would be nice tutorial as well.

    Reply

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.