title: Undefined column selected in R
body: <p>The error I got was <strong>"Error in '[.data.frame'(current_dataset, complete.cases(current_dataset)) :undefined columns selected".</strong> I tried to find the problem but can't figure it out. </p>
  
  <p><strong>What I want the function to do:</strong> First, it is goes through several files that contain sulfate and nitrate information for different locations. These files all contain 'csv' so myfiles will be used as a vector to easily refer to files. Then I want to loop through the 332 files, read it, and check if there are enough complete cases (this number is an argument in the function). If that's the case, I want to add all complete cases (sulfate and nitrate data) to a data frame that was defined previously. Finally I want to return the correlation between sulfate and nitrate. </p>
  
  <pre><code>corr &lt;- function(directory, threshold = 0) {
      #store data frame that holds sulfate amount and nitrate amount that meet threshold and are complete cases
      data &lt;- data.frame(sulfate = numeric(0), nitrate = numeric(0))
  
      #set working directory
      setwd(directory)
  
      #get file names
      myfiles &lt;- list.files(pattern = "csv")
  
      #loop through files
      for(i in 1:332) {
  
          #read each file
          current_dataset &lt;- read.csv(myfiles[i])
  
          #check if there are enough compelte cases to meet threshold
          if(sum(complete.cases(current_dataset)) &gt; threshold) {
  
              #get complete cases
              complete_cases &lt;- current_dataset[complete.cases(current_dataset)]
  
              #add sulfate and nitrate info to table
              data &lt;- rbind(data, data.frame(sulfate = complete_cases$sulfate[i], nitrate = complete_cases$nitrate)[i])
          }
      }
      #get correlation
      cor(data)
  }
  </code></pre>