title: Looping a function over multiple files
body: <p>I wrote a simple function:</p>
  
  <pre><code>myfunction &lt;- function(fileName, stringsAsFactors=TRUE,                 
                     check.names=FALSE,               
                     skip =1,...) {
  Data &lt;- read.delim(fileName, skip = skip,                     
                   stringsAsFactors=stringsAsFactors,                      
                   check.names = check.names, ...)                   
  cb    &lt;- list()                       
  Index &lt;- as.numeric(as.factor(Data[,1]))              
  cb    &lt;-  cbind(Data, Index)              
  return(cb)
  }
  </code></pre>
  
  <p>This function takes the first column of the file named Data, create an Index according to that first column and then cbind the file Data and the index created.</p>
  
  <p>This function will be applied in file named: <code>myfile_00.txt</code>, <code>myfile_01.txt</code> and so on. For one single file it looks like:</p>
  
  <pre><code>myfunction (fileName = "myfile_00.txt")
  myfunction (fileName = "myfile_01.txt")
  .......        
  </code></pre>
  
  <p>I have around 1000 files so I suppose, the loop can be as from another post:</p>
  
  <pre><code>mytxt  &lt;- dir(pattern=".txt")      
  n      &lt;- length(mytxt)      
  mylist &lt;- vector("list", n)   
  for(i in 1:n) {
      mylist[[i]] &lt;- read.delim(mytxt[i], header = F, skip = 1)
  }
  </code></pre>
  
  <p>then:     </p>
  
  <pre><code>d &lt;- lapply(mylist, myfunction)
  </code></pre>
  
  <p>Unfortunately it does not work... When using <code>lapply</code> an error occurs: </p>
  
  <pre><code>Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  'file' must be a character string or connection
  </code></pre>
  
  <p>Since I' m new in R probably I' m doing mistakes I'm not able to figure out. </p>