title: Getting error using cor() in For loop in R for list of csv files
body: <p>I have a list of csv files that I'm iterating through</p>
  
  <pre><code>corr &lt;- function(directory) {
          filename &lt;- list.files(directory)
          x &lt;- paste(directory, "/", sep="")
          monitor_list &lt;- paste(x, filename, sep="")
          completeLocations &lt;- NULL
          corrData &lt;- NULL
  </code></pre>
  
  <p>I'm trying to use cor() to generate another data frame that just has the cor values for 2 columns in each csv file. </p>
  
  <pre><code>  for (monitor in monitor_list) {
              locationInfo &lt;- read.csv(monitor)
              corrData &lt;- rbind(corrData, cor(locationInfo$sulfate, locationInfo$nitrate, use = "complete.obs"))
              completeLocations &lt;- rbind(completeLocations, sum(complete.cases(locationInfo)))
  
  
          }
  </code></pre>
  
  <p>And it throws this error  </p>
  
  <blockquote>
    <p>Error in cor(locationInfo$sulfate, locationInfo$nitrate, use =
    "complete.obs") :    no complete element pairs</p>
  </blockquote>
  
  <p>I know this can be done because when I run the exact same thing outside of the loop, it return the correlation for the last csv file in the list. </p>
  
  <pre><code> print(cor(locationInfo$sulfate, locationInfo$nitrate, use = "complete.obs"))
  </code></pre>
  
  <p>I'm relatively new to R and I think I'm missing something inside of the loop, I'm just not sure what it is...</p>