title: Trying to understand R error: Error in FUN(X[[i]], ...) : only defined on a data frame with all numeric variables
body: <p>I've been getting this error message and traceback:</p>
  
  <pre><code>Error in FUN(X[[i]], ...) : 
    only defined on a data frame with all numeric variables 
  
  5 stop("only defined on a data frame with all numeric variables") 
  4 FUN(X[[i]], ...) 
  3 lapply(args, function(x) {
      x &lt;- as.matrix(x)
      if (!is.numeric(x) &amp;&amp; !is.complex(x)) 
          stop("only defined on a data frame with all numeric variables") ... 
  2 Summary.data.frame(structure(list(Date = structure(c(279L, 285L, 
  291L, 297L, 303L, 315L, 321L, 327L, 333L, 339L, 345L, 357L, 363L, 
  369L, 375L, 387L, 393L, 399L, 405L, 417L, 423L, 429L, 435L, 441L, 
  447L, 453L, 477L, 501L, 555L, 561L, 567L, 573L, 579L, 585L, 591L,  ... 
  1 corr("specdata") 
  </code></pre>
  
  <p>and from my research it seems that it means that there is non-numeric data in my data set.  The data set I'm using is from the Coursera course, and if that were the case, I'd assume others would be having the same problem as I am, but there doesn't seem to be any mentions in any of the discussion boards or online of a similar problem.  My only guess is that it is a result of my function code, which is below:</p>
  
  <pre><code>corr &lt;- function(directory, threshold = 0) {
  
  vect1 &lt;- numeric()
  files_list &lt;- list.files(directory, full.names = TRUE)
  
  for (i in 1:332) {
  
      data &lt;- read.csv(files_list[i])
      good &lt;- complete.cases(data)
      complete_data &lt;- data[good,]
      sulfate &lt;- complete_data[,2]
      nitrate &lt;- complete_data[,3]
  
          if (sum(complete_data) &gt;= threshold) {
              b &lt;- cor(sulfate,nitrate)
              vect1 &lt;- rbind(b)
          }
          else vect1 &lt;- (numeric())
      }
      return(vect1)
  }
  </code></pre>
  
  <p>From the error message and the traceback I "think" the error is occurring when the correlation is running on the sulfate and nitrate columns.  When I've run the code on just the first file in the directory, it runs fine with no error messages. Any help or insight as to why this error is occurring, and how to fix it would be helpful. </p>
  
  <p>I have tried to coerce the dataset into being numeric - </p>
  
  <pre><code>complete_data &lt;- as.numeric(data[good,])
  </code></pre>
  
  <p>but I get a different error message back "Error: (list) object cannot be coerced to type 'double'"</p>