Append data to data frame in a loop - function only returns last row of data frame
<p>I want to create a function which loops through a large number of files, calculates the number of complete cases for each file and then appends a new row to an existing data frame with the "ID" number of the file and its corresponding number of complete cases. </p>

<p>Below I have created a code which only returns the last row of the data frame. I belive my function only returns the last row, because R overwrites my data frame in every loop, but I am not sure. I have done a lot of research online how to solve this, but I could not find an easy solution (I am very very new to R). </p>

<p>Below you can see my code and the output I get: </p>

<pre><code>complete &lt;- function(directory = "specdata", id = 1:332) {
  files_list &lt;- list.files("specdata", full.names = T) # creates a list of files

  dat &lt;- data.frame() # creates an emmpty data frame

    for (i in id) {

    data &lt;- read.csv(files_list[i]) # reads the file "i" in the id vector 

    nobs &lt;- sum(complete.cases(data)) # counts the number of complete cases in that file  

    data_frame &lt;- data.frame("ID" = i, nobs) # here I want to store the number of complete cases in a data frame

    output &lt;- rbind(dat, data_frame) # here the data_frame should be added to an existing data frame
  }

  print(output)
}
</code></pre>

<p>When I run <code>complete( , 3:5)</code>, I get the following result: </p>

<pre><code>  ID nobs
1  5  402
</code></pre>

<p>Thanks four your help! :)</p>