title: Use for loop to bind multiple text files together from different subfolders in R
body: <p>I have seen this question asked before, but they don't quite help in tackling my problem.</p>
  
  <p>I am looking to bind (<code>rbind</code>) multiple text files from 150 sub folders, but I ONLY am interested in 2 different files in each folder. Here are details:</p>
  
  <ul>
  <li>Main folder called "Folder" which contains 150 subfolders</li>
  <li>In each subfolder, I want to pull out the files that start with 1 and 11. (i.e. 1_HDx1.txt and 11_HDx1.txt) - there are 2 files of these in each folder</li>
  <li>Create a column in the data frame that has the name of the subfolder the files were pulled from.</li>
  </ul>
  
  <p>I know how to do this if all files were in one folder (see directly below), but can I do something similar for a folder of folders??</p>
  
  <pre><code>z &lt;- NULL
  files &lt;- dir("Folder")
  for (file in files) {
    x &lt;- read.csv(file.path("Folder", file), as.is=TRUE)
    x$source &lt;- substring(file, 8, 10) #name of file for the source
   z &lt;- rbind(z, x)]) 
  }
  </code></pre>
  
  <p>File structure for first 3 subfolders out of 150:</p>
  
  <pre><code>Folder
  - WSTNUM_001
    - 1_HDx
    - 2_LDx
    - 3_LD
    - 4_LD
    - 5_ld
    .....
    - 11_Urbanx
    - 12_Urbany
  - WSTNUM_002
    - 1_HDx
    - 2_LDx
    - 3_LD
    - 4_LD
    - 5_ld
    .....
    - 11_Urbanx
    - 12_Urbany
  - WSTNUM_003
    - 1_HDx
    - 2_LDx
    - 3_LD
    - 4_LD
    - 5_ld
    .....
    - 11_Urbanx
    - 12_Urbany`
  </code></pre>