title: Write output of R loop to file
body: <p>Currently my R loop (see below) overwrites itself during each iteration. I want to output the result from each loop into a text file.</p>
  
  <p>In more detail:
  R beginner here wondering how to include a line in my script so that the mean value calculating for each file is written to a text file. So that the script creates a new empty text file and then each time the script calculates the mean of column 4 in a file that value is output to a row in the text file which contains the name of the file in column1 and the mean value in column 2. 
  Thanks for your help!</p>
  
  <pre><code>filename &lt;- system("ls /dir/",intern=TRUE)
  
  for(i in 1:length(filename)){
  
  file &lt;- read.table(filename[i],header=FALSE) ## if you have headers in your files ##
  mean &lt;- mean(as.numeric(file$V4))
  
  
  
  }
  </code></pre>