This post presents basic R code snippets to read files with given file extensions such as csv or txt. This is simple but very useful when it comes to the case where there are too many files to read manually.
If we have too many (i.e. 1000 files) csv files or its variants, it is impossible to read these files one by one manually.
For example, let’s assume that there are following 6 files in a target directory. The first four files (csv, CSV, txt, TXT) are the files that we want to read. The contents of these files are straightforward because the output will show these contents in the later.
In this case, we can use list.files() R function to read these files with some certain file extensions.
list.files() is a built-in R function which returns a list of names of files with a given pattern.
ignore.case = TRUE, full.names = FALSE)
In the above R command, “\\.(csv|txt)$” pattern specifies that 1) it is applied at the end of file name($), 2) multiple file extensions such as csv or txt file ((csv|txt)) are allowed but not for similar extensions such as ccsv or ttxt(\\.). csv and CSV or txt and TXT are allowed because case sensitivity is ignored (ignore.case = TRUE).
The following R code is easy and self-contained: 1) reads each csv (CSV) or txt (TXT) files and make each data.frame separately and 2) reads and collects them into one data.frame.
We can find that only 4 files with correct file extensions are read while 2 unwanted files (.CCSV and .ttxt) are ignored.
This R code is efficient and useful especially when there are too many files to read. \(\blacksquare\)