title: Import all txt files in folder, concatenate into data frame, use file names as variable in R?
body: <p>I have a folder with 142 tab-delimited text files. Each file has 19 variables, and then a number of rows beneath (usually no more than 30 rows, but it varies).
  I want to do several things with these files in R automatically, and I can't seem to get exactly what I want with my code. I am new to loops, I got both sections of code from previous posts here at stackoverflow but can't seem to figure out how to combine their functions.</p>
  
  <ol>
  <li><p>I want to turn the filename into a variable when reading the files into R, so that each row has the identifying file name</p></li>
  <li><p>Concatenate all files (with filename variable and no header) into one dataframe with dimensions Yx19, where Y=however many resulting rows there are. </p></li>
  </ol>
  
  <p>I am able to create a list of the 142 dataframes using this code:</p>
  
  <pre><code>myFiles = list.files(path="~/Documents/ForR/", pattern="*.txt")
  data &lt;- lapply(myFiles, read.table, sep="\t", header=FALSE)
  names(data) &lt;- myFiles
      for(i in myFiles) 
      data[[i]]$Source = i
      do.call(rbind, data)
  </code></pre>
  
  <p>I am able to create the dataframe I want with 19 variables, but the filename is not present:</p>
  
  <pre><code>files &lt;- list.files(path="~/Documents/ForR/.", pattern=".txt")
      DF &lt;- NULL
          for (f in files) {
          dat &lt;- read.csv(f, header=F, sep="\t", na.strings="", colClasses="character")
          DF &lt;- rbind(DF, dat)
      }
  </code></pre>
  
  <p>How do I add the file name (without .txt if possible) as a variable to the loop?</p>