title: Only last iteration of loop is saved
body: <p>I have a list of dataframes (subspec2) which I want to loop through to get the columns with the maximum value from each dataframe, and write these to a new dataframe. I wrote the following loop:</p>
  
  <pre><code>good.data&lt;-data.frame(matrix(nrow=401, ncol=78)) #create empty dataframe
  
  for (i in length(subspec2)) ##subspec2 is the list of dataframes
  
  {
  
  max.name&lt;-names(which.max(apply(subspec2[[i]],MARGIN=2,max))) #find column name with max value
  good.data[,i]&lt;-subspec2[[i]][max.name] #write the contents of this column into dataframe
  
  }
  </code></pre>
  
  <p>This seems to work but only returns values in the last column, nothing else appears to have been saved. Many threads point out the df must be outside the loop, but that is not the problem here.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>Thank you!</p>