maximusdooku maximusdooku - 4 months ago 18
R Question

How can I search for a particular string in a folder full of files and import it?

I have a folder full of files with filenames such as these.

[1] "ts.01382800.crest.csv" "ts.01383500.crest.csv" "ts.01384500.crest.csv" "ts.01386000.crest.csv"
[5] "ts.01387000.crest.csv" "ts.01387400.crest.csv" "ts.01387420.crest.csv" "ts.01387450.crest.csv"
[9] "ts.01387500.crest.csv" "ts.01387908.crest.csv"

I need to do one operation again and again. Basically search for a particular string (Say, 1382800), find the filename with the match and import it. Please note, the name may not be exact match as the preceding zero may not exist sometimes.

Currently, I am importing a list of the files in the folder using list.files, using grep on that list to find a filename, then re-constructing the file path and importing it. There must be an easier way to do it.


This code filter all the data contains 1382800

 ls <- list.files(path='~/',pattern="1382800")