title: Return a data frame from function
body: <p>I have the following code inside a function</p>
  
  <pre><code>Myfunc&lt;- function(directory, MyFiles, id = 1:332) {
  # uncomment the 3 lines below for testing
  #directory&lt;-"local"
  #id=c(2, 4)
  #MyFiles&lt;-c(f2.csv,f4.csv)
  idd&lt;-id
  
  df2 &lt;- data.frame()
  
  for(i in 1:length(idd)) {
    EmptyVector &lt;- read.csv(MyFiles[i])  
    comp_cases[i]&lt;-sum(complete.cases(EmptyVector))
    print(comp_cases[[i]])
    id=idd[i]
    ret2=comp_cases[[i]]
    df2&lt;-rbind(df2,data.frame(id,ret2))
   }
  print(df2)
  return(df2)
  }
  </code></pre>
  
  <p>This works when I try to run it in R by selecting the code inside the function and commenting out the return. I get a nice data frame like from the print statement:</p>
  
  <pre><code>&gt; df2
   id ret2
  1 2  994
  2 4  7112
  </code></pre>
  
  <p>However, when I try to return the dataframe <code>df2</code> from the function it only returns the 1st row, ignoring all other values. My problem is that it works within the function for various values I have tried (opening multiple files with various combinations) and not when I try to return the data frame. Can someone help please. Thanks a lot in advance.  </p>