title: Read all files in a folder and apply a function to each data frame
body: <p>I am doing a relatively simple piece of analysis that I have put into a function on all the files in a particular folder. I was wondering whether anyone had any tips to help me automate the process on a number of different folders.</p>
  <ol>
  <li>Firstly, I was wondering whether there was a way of reading all the files in a particular folder straight into R. I believe the following command will list all the files:</li>
  </ol>
  <p><code>files &lt;- (Sys.glob(&quot;*.csv&quot;))</code></p>
  <p>...which I found from <a href="https://stackoverflow.com/questions/4876813/using-r-to-list-all-files-with-a-specified-extension">Using R to list all files with a specified extension</a></p>
  <p>And then the following code reads all those files into R.</p>
  <pre><code>listOfFiles &lt;- lapply(files, function(x) read.table(x, header = FALSE)) 
  </code></pre>
  <p>…from <a href="https://stackoverflow.com/questions/6420207/manipulating-multiple-files-in-r">Manipulating multiple files in R</a></p>
  <p>But the files seem to be read in as one continuous list and not individual files… how can I change the script to open all the csv files in a particular folder as individual dataframes?</p>
  <ol start="2">
  <li><p>Secondly, assuming that I can read all the files in separately, how do I complete a function on all these dataframes in one go.  For example, I have created four small dataframes so I can illustrate what I want:</p>
  <pre><code> Df.1 &lt;- data.frame(A = c(5,4,7,6,8,4),B = (c(1,5,2,4,9,1)))
   Df.2 &lt;- data.frame(A = c(1:6),B = (c(2,3,4,5,1,1)))
   Df.3 &lt;- data.frame(A = c(4,6,8,0,1,11),B = (c(7,6,5,9,1,15)))
   Df.4 &lt;- data.frame(A = c(4,2,6,8,1,0),B = (c(3,1,9,11,2,16)))
  </code></pre>
  </li>
  </ol>
  <p>I have also made up an example function:</p>
  <pre><code>Summary&lt;-function(dfile){
  SumA&lt;-sum(dfile$A)
  MinA&lt;-min(dfile$A)
  MeanA&lt;-mean(dfile$A)
  MedianA&lt;-median(dfile$A)
  MaxA&lt;-max(dfile$A)
  
  sumB&lt;-sum(dfile$B)
  MinB&lt;-min(dfile$B)
  MeanB&lt;-mean(dfile$B)
  MedianB&lt;-median(dfile$B)
  MaxB&lt;-max(dfile$B)
  
  Sum&lt;-c(sumA,sumB)
  Min&lt;-c(MinA,MinB)
  Mean&lt;-c(MeanA,MeanB)
  Median&lt;-c(MedianA,MedianB)
  Max&lt;-c(MaxA,MaxB)
  rm(sumA,sumB,MinA,MinB,MeanA,MeanB,MedianA,MedianB,MaxA,MaxB)
  
  Label&lt;-c(&quot;A&quot;,&quot;B&quot;)
  dfile_summary&lt;-data.frame(Label,Sum,Min,Mean,Median,Max)
  return(dfile_summary)}
  </code></pre>
  <p>I would ordinarily use the following command to apply the function to each individual dataframe.</p>
  <p>Df1.summary&lt;-Summary(dfile)</p>
  <p>Is there a way instead of applying the function to all the dataframes, and use the titles of the dataframes in the summary tables (i.e. Df1.summary).</p>
  <p>Many thanks,</p>
  <p>Katie</p>