Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

• USD.CSV
• EUR.csv
• CNY.TXT
• AUD.txt
• CNY2.ttxt
• USD.CCSV

In this case, we can use list.files() R function to read these files with some certain file extensions.

### list.files() function

list.files() is a built-in R function which returns a list of names of files with a given pattern.

### Output

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$$