title: How to export values in a self-defined function to predefined dataframe in r
body: <p>I am running a self-defined <code>function</code> and I want to extract some values from the function to a predefined <code>dataframe</code>. This function will be applied to a list. Each element of the list should make one row of the predefined <code>dataframe</code>.
  Please see the code below.</p>
  
  <p>I predefined a <code>dataframe</code> with four variable ahead of the <code>function</code></p>
  
  <pre><code>
  df &lt;- data.frame(Year = as.integer(),
                   Total_sites = as.integer(),
                   Sites_25 = as.integer(),
                   Missing_hours = as.double())
  </code></pre>
  
  <p>Below is part of the function. </p>
  
  <pre><code>fun &lt;- function(a_csv, the_dir) {
  
    hcount &lt;- dfAOT40 %&gt;% group_by(IDS) %&gt;% 
      summarise(count = n()) 
  
    hcount_less &lt;- hcount %&gt;% filter(count &lt; 897)
  
    year &lt;- substring(dfoz$'Date Local'[1], 1, 4)  # first variable in the dataframe
  
    total_sites &lt;- length(unique(hcount$IDS)) # second variable variable in the dataframe
  
    sites_25 &lt;- length(unique(hcount_less$IDS)) # third variable in the dataframe
  
    missing &lt;- sum(hcount$count) / (total_sites * 1196)  # fourth variable in the dataframe
  
    df &lt;- data.frame(Year = as.integer(),
                     Total_sites = as.integer(),
                     Sites_25 = as.integer(),
                     Missing_hours = as.double())
  
    df[1,] &lt;-  c(year, total_sites, sites_25, missing) # each element should make one row of the dataframe.
  
    return(df)
  
  }
  
  
  
  </code></pre>
  
  <p>The problem was solved by the code below. </p>
  
  <p><code>df &lt;- do.call(rbind, lapply(files, FUN = fun, the_dir))</code></p>