title: Looping through files in R
body: <p>I am using R to calculate the mean values of a column in a file like so:</p>
  
  <pre><code>R
  file1 = read.table("x01")
  mean(file1$V4)
  </code></pre>
  
  <p>However I have no experience building loops involving R, only with bash.
  How would I convert this into a loop that did this for every file in a folder and saved the output into one file with the file name and mean value as the 2 columns for each row?
  eg:</p>
  
  <pre><code>x01(or file1 if that is simpler) 23.4
  x02 25.4
  x03 10.4
  </code></pre>
  
  <p>etc</p>
  
  <p>(Don't mind if the solution is bash and R or exclusively R)
  Many thanks for your help!</p>
  
  <p>Current error from one of the solutions using bash and R:</p>
  
  <pre><code>Error in `[.data.frame`(read.table("PercentWindowConservedRanked_Lowest_cleanfor1000genomes_1000regions_x013",  : 
    undefined columns selected
  Calls: mean -&gt; [ -&gt; [.data.frame
  Execution halted
  </code></pre>